One Dimensional Arrays, Specify Size During Execution - Notes | CS 110, Study notes of Computer Science

Material Type: Notes; Professor: Tanner; Class: Introduction-Computer Science; Subject: Computer Science; University: West Virginia University; Term: Unknown 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-hcb
koofers-user-hcb 🇺🇸

8 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 110 One Dimensional Arrays 1
Copyright C. Tanner 2009
Array
o Collection of data
o Data strucutre
o Fixed number of items
o Each called an element
o All same data type
o Use them
Visit the data more than once
Find those values > ave
One dimensional array
o dataType [] name
o name = new dataType[size]
o elements 0-size-1
o to access an element use index (subscript)
non-negative integer < size
o [] array subscripting operator
Specify size during execution
o int size;
o size = sc.nextInt();
o int [] a = new int[size];
initialize an array
o double [] = { 1.2,3.4,5.6};
o size is determined by the number of elements
o String [] daysWeek = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”,
“Thursday”, “friday”, “Saturday”};
o int [] primes={2,3,5,7,11,13,17,19,23,29};
o print the values in primes
System.out.print (“the prime numbers < 30”);
for (int i =0; i<primes.length;i++)
System.out.print (primes[i] + “’ “);
length operator
o data field of an array
o not a method
o tells the number of elements in the array
o size
Processing arrays
o For loop where the loop variable iterates through the elements of the
array
Initial 0
Increment 1
Variable < length
Write a loop which will intitalize an array to -1
for (int i = 0; i < array.length; i++)
array[i] = -1;
pf3
pf4
pf5

Partial preview of the text

Download One Dimensional Arrays, Specify Size During Execution - Notes | CS 110 and more Study notes Computer Science in PDF only on Docsity!

  • Array o Collection of data o Data strucutre o Fixed number of items o Each called an element o All same data type o Use them ƒ Visit the data more than once - Find those values > ave
  • One dimensional array o dataType [] name o name = new dataType[size] o elements 0-size- o to access an element use index (subscript) ƒ non-negative integer < size o [] array subscripting operator
  • Specify size during execution o int size; o size = sc.nextInt(); o int [] a = new int[size];
  • initialize an array o double [] = { 1.2,3.4,5.6}; o size is determined by the number of elements o String [] daysWeek = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “friday”, “Saturday”}; o int [] primes={2,3,5,7,11,13,17,19,23,29}; o print the values in primes System.out.print (“the prime numbers < 30”); for (int i =0; i<primes.length;i++) System.out.print (primes[i] + “’ “);
  • length operator o data field of an array o not a method o tells the number of elements in the array o size
  • Processing arrays o For loop where the loop variable iterates through the elements of the array ƒ Initial 0 ƒ Increment 1 ƒ Variable < length
  • Write a loop which will intitalize an array to - for (int i = 0; i < array.length; i++) array[i] = -1;
  • Write a loop that will read data from keyboard in to the array for (int i = 0; i < array.length; i++) array[i] = sc.nextInt();
  • Write a loop that will display the values of the array one per line for (int i =0; I < array.length; i++) System.out.println(array[i]);
  • Write a loop which will find the sum of the elements of the array and then find the average sum =0; for (int i=0; i< array.lenght;i++) sum += array[i]; ave = sum/array.length;
  • Write a loop that will find the max and min of the elements max = 0; min = 100; for (int i =0; i < array.length; i++) { if (max < array[i]) max = array[i]; if (min > array[i]) min = array[i]; }

System.out.println (“the max is: “ + max); System.out.println (“the min is: “ + min);

  • Array out of Bounds exception o If you try to reference an element which does not exist ƒ Subscript < 0 or > size- ƒ Raise ArrayOutOfBounds exception - Program terminates

Write the same method for arrays of strings:

  • Write a method which will create a copy of an array o Deep copy

int [] createCopy (int [] a) { int [] copy = new int [a.length];

for (int i =0; i< a.length; i++) copy [i]= a[i]; return copy; }

Consider arrays a,b. and c a = b+c;

not defined in java….it is defined in MATLAB

int [] addArrays (int [] a, int []b) { int [] sum = new int [a.length]; if (a.length != b.length) System.out.println(“can’t add arrays they are not of the same length”); else { for (int i=0; i<a.length;i++)

sum[i]=a[i]+b[i]; }

return sum; }

  • Passing arrays to methods o Pass by reference o Values can be changed by the module
  • Returning an array o Returns the reference
  • Write a method to reverse the elements of the array
  • Arrays of Obkects o String example o Each element of the array is a reference

Names Æ 0 ----Æ Joe 1 ---Æ Sally 2 ---Æ Susie 3 ---Æ John

Must use the objects methods to manipulate it

names[0].equals(“Sam”)