80 likes | 260 Views
Arrays. Definition: An indexed table of variables in memory. Declare the Array of 100 elements Integers: int [] integers = new int [100]; Strings: String[] strings = new String[100]; Doubles: double[] doubles = new double[100]; Declare and initialize at the same time (initialization list)
E N D
Arrays Definition: An indexed table of variables in memory • Declare the Array of 100 elements • Integers: int[] integers = new int[100]; • Strings: String[] strings = new String[100]; • Doubles: double[] doubles = new double[100]; • Declare and initialize at the same time (initialization list) • Six integers: int[] integers = {3, 4, 5, 6, 7, 8}; • Four Strings: String[] strings = {“abc”, “def”, “ghi”, “jkl”}; • Five doubles: Doubles[] doubles = {3.3, 4.4, 5.5, 6.6, 7.7}; • Notes • Without initialization lists, it would take seven statements to initialize the array (see next slide) • String[] strings = new String[100] puts a null in each spot in the table. null is a reserved word meaning nothing is there yet.
Array Example • int nums[] = new int[6];nums[0] = 10;nums[1] = 20;nums[2] = 22;nums[3] = 33;nums[4] = 50;nums[5] = 66; • int nums[] = {10,20,22,33,50,66}; • What prints?System.out.println(nums[4]); • Replace an elementnums[3] = 99; • Note: Java arrays first element always is index zero. • Find an array’s lengthSystem.out.println(nums.length) Index Content 10 20 22 nums 33 50 66
Two column (dimensioned) array twoCols Access and print row 3 column 4:System.out.println(twoCols[3][4]); prints a 7 Replace the 76 in row 6, column 0 with a 63:twoCols[6][0] = 63; Declareint[][] twoCols = { {11,22,33,44,55}, {1,99,88,77,66}, {2,3,4,5,6}, {23,12,9,8,7}, {34,45,56,67,89}, {65,54,43,32,21}, {76,87,13,24,35} }; Note: initialization lists are a second use of {} in Java
Why Arrays? Answer: With counter-controlled loops, we can do lots with few statements! • Fill a one column array with a valuefor (int i=0; i<nums.length; i++) nums[i] = 0; • Sum up the elements in an arrayint sums = 0;for (int i=0; i<nums.length; i++) { sum += nums[i]; }System.out.println(sum); • Find the biggest number in the arrayint max = nums[0];for (int i=0; i<nums.length; i++) if (max < nums[i]) max = nums[i];System.out.println(“Maximum is “ + max); Question: Why doesn’t the if statement in the for loop need braces around it?
Example: Member of a club String person = IO.readString(“What’s your name”); for (int i=0; i<names.length; i++) { found = false; if (person.equals(names[i])){ found = true; break; } } if (found) System.out.prinltn(“OK to enter”); else System.out.println(“Lock them up”); • We have an array of Strings • Each string is the name of a person in the Computer Science club • We need to know if someone should be allowed admission • How do we determine if that person is in the member list? Question: Why compare with person.equals(names[i]) and not == names[i]?
Example: Is it a magic square? • A magic square is one where all the rows, columns, and diagonals sum to the same total. • We want to see if a two-column array magic is a magic square. • The example on the right tests the rows • How would we test the columns? • How would we test the diagonals? boolean magicS = true; int sum, magicTotal = 0; for (int row = 0; row<magic.length; row++) { sum = 0; for (int c=0; c<magic[c].length; c++) sum += magic[row][c]; if (row==0) magicTotal = sum;else if (magicTotal!=sum) { magicS= false; break;} } if (magicS) System.out.println(“Rows OK”); else System.out.println(“Rows No Good”);
Putting it all together Enter a series of grades and compute the high, low and average score public static void main(String[] args) { int count = 0, low = 0, high = 0, score; double total = 0, avg; int[] scores = new int[100]; while ( (count<100) && ( (score=IO.readInt(“Enter score”)) > 0) ) { scores[count++] = score; } // What happens if we allowed count = 100? if (count>0) { min = max = scores[0]; for (int i=0; i<count; i++) { total += scores[i]; if (scores[i] < min) min = scores[i]; if (scores[i] > max) max = scores[i]; } avg = total/count; System.out.println(“min = ” + min + “ max = ” + max + “avg = ” + avg); } } Question: Why do we need to make sure that count is greater than zero?
Review • What is an array? • Why are arrays useful? • What is an initializion list? Why are they useful? • What is the index to an array? • What is the highest index to an array of ten elements? • What is an array’s dimension? • What is the purpose of the new reserved word? • What is an array element? • Explain the syntax of the for loop construct. • How do you determine the size of a single dimensioned array? How about the second dimension of a two dimensioned array? • How do you store into an array? • How do you retrieve data from an array?