Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Declare the Array of 100 elements Notes

Similar presentations


Presentation on theme: "Arrays Declare the Array of 100 elements Notes"— Presentation transcript:

1 Arrays Declare the Array of 100 elements Notes
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 (initializer 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 initializer 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.

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) Index Content 1 2 3 4 5 10 20 22 nums 33 50 66

3 Two column (dimensioned) array
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: initializer lists are a another use of {} to enclose initial array element values Two column (dimensioned) array twoCols 1 2 3 4 11 22 33 44 55 99 88 77 66 5 6 23 12 9 8 7 34 45 56 67 89 65 54 43 32 21 76 87 13 24 35 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;

4 Why Arrays? Answer: With counter-controlled loops, we can do lots with few statements! 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); Question: Why doesn’t the if statement in the for loop need braces around it?

5 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]?

6 Example: Is it a magic square?
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”); 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?

7 Putting it all together
Enter a series of grades and compute the high, low and average score public class Test { public static void main(String[] args) { int count = 0; double total = 0, avg, min, max, scores[] = new double[100], score, low, high; while ((count<100) && ( (score=IO.readDouble("Enter score, -1 to quit")) > 0) ) { scores[count++] = score; } // What happens if we allowed if (count==0) System.exit(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.printf("min=%.2f max=%.2f avg=%.2f",min, max, avg); } } Question: Why do we need to make sure that count is greater than zero?

8 Review What is an array? Why are arrays useful?
What is an initializer 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?


Download ppt "Arrays Declare the Array of 100 elements Notes"

Similar presentations


Ads by Google