Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]

Similar presentations


Presentation on theme: "Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]"— Presentation transcript:

1 Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[] doubles = new double[100]; Declare and initialize at the same time (initialization list) 1.Six integers: int[] integers = {3, 4, 5, 6, 7, 8}; 2.Four Strings: String[] strings = {“abc”, “def”, “ghi”, “jkl”}; 3.Five doubles: Doubles[] doubles = {3.3, 4.4, 5.5, 6.6, 7.7}; Notes 1.Without initialization lists, it would take seven statements to initialize the array (see next slide) 2.String[] strings = new String[100] puts a null in each spot in the table. null is a reserved word meaning nothing is there yet. Definition: An indexed table of variables in memory

2 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 element nums[3] = 99; Note: Java arrays first element always is index zero. Find an array’s length System.out.println(nums.length) 0 1 2 3 4 5 Index 10 66 50 33 22 20 Content nums

3 Two column (dimensioned) array 01234 01122334455 1199887766 223456 32312987 43445566789 56554433221 67687132435 1.Access and print row 3 column 4: System.out.println(twoCols[3][4]); prints a 7 2.Replace the 76 in row 6, column 0 with a 63: twoCols[6][0] = 63; Declare int[][] 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 twoCols

4 Why Arrays? Fill a one column array with a value for (int i=0; i<nums.length; i++) nums[i] = 0; Sum up the elements in an array int sums = 0; for (int i=0; i<nums.length; i++) {sum += nums[i]; } System.out.println(sum); Find the biggest number in the array int max = nums[0]; for (int i=0; i<nums.length; i++) if (max < nums[i]) max = nums[i]; System.out.println(“Maximum is “ + max); Answer: With counter-controlled loops, we can do lots with few statements! Question: Why doesn’t the if statement in the for loop need braces around it?

5 Example: Member of a club 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? 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”); Question: Why compare with person.equals(names[i]) and not == names[i]?

6 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”);

7 Putting it all together 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 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); } Enter a series of grades and compute the high, low and average score Question: Why do we need to make sure that count is greater than zero?

8 Review 1.What is an array? 2.Why are arrays useful? 3.What is an initializion list? Why are they useful? 4.What is the index to an array? 5.What is the highest index to an array of ten elements? 6.What is an array’s dimension? 7.What is the purpose of the new reserved word? 8.What is an array element? 9.Explain the syntax of the for loop construct. 10.How do you determine the size of a single dimensioned array? How about the second dimension of a two dimensioned array? 11.How do you store into an array? 12.How do you retrieve data from an array?


Download ppt "Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]"

Similar presentations


Ads by Google