Download presentation
Presentation is loading. Please wait.
Published byLorena Wiggins Modified over 9 years ago
1
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
2
CS100Lecture 102 Today’s Topics Review Arrays –Allocation/Deallocation –Subscripting –Use of arrays to store large collections of data –Several examples
3
CS100Lecture 103 Review of iteration For loops –for(j = 0; j < NUM; j++) System.out.println(j); –What do you think for(;;) does, for example? Invariants break and continue statements
4
CS100Lecture 104 Hypothetical Problem Input: zero or more grades from 0 to 100 preceded by the number of grades 5 9085408912 Task: read grades and compile information about them –print them in reverse order –print them in increasing order –print a histogram So, need to read in all the values before processing, need as many variables as grades... How to do this using the Java we know so far?
5
CS100Lecture 105 Use arrays instead! An array is an ordered list of values. Each value is stored at a position in the array The number referring to the position is called an index In Java, array indices begin at 0 40 85 89 12 90 g 0 1 2 3 4
6
CS100Lecture 106 Deconstruction of this array g[0] is 90 g[1] is 85 g[2] is 40 g[3] is 89 g[4] is 12 g.length is the number of elements in the array, g.length is 5 In “g[4]”, 4 is the index or subscript
7
CS100Lecture 107 Another example h.length is the number of array elements in array h -- here, h.length is 323 -32 54 -101 82 1 h 0 1 2 3 4... 93 322
8
CS100Lecture 108 Some notation segment number of values in it empty when g[h..i-1] i-h h = i g[i..j] j+1-ij = i-1 g[j+1..k-1] k-(j+1) j = k h i j k g
9
CS100Lecture 109 Declaring an array variable int[] g; g float[] averages; averages Employee[] employees; employees Declaring a variable does not “allocate” or create the array of elements; it only declares a variable that can contain a reference to an array of elements. An array is much like a class in that an array variable contains a reference to an array. null
10
CS100Lecture 1010 How to get the space? g = new int[5]; g Employee[] e = new Employee[206]; e 0 1 2 3 4 null null null null null... null null 0 1 2 3 4 … 204 205
11
CS100Lecture 1011 More on declaration and accessing What happens in the statement e[3] = new Employee(“Millett”, 1999); Given allocated array e, we can reference e[0], e[1] …., but we can also use expressions as array indices: e[2*b], e[i], etc.
12
CS100Lecture 1012 Using array elements Suppose g is an array of integers Then, g[i] where i is in range can be used just as an integer variable –g[i] = 3; a = g[i]*2; c.setX(g[i]); –System.out.println(“value is ” + g[i]); Note that arrays are objects -- therefore have the same ‘call by value’ conventions Java does bounds checking -- throws exception if out of bounds
13
CS100Lecture 1013 Example program with arrays // Read in a list of integer grades, preceded by the // number of grades, and print in them in reverse order int n= Integer.parseInt(stdin.readLine()); // number of grades int[ ] g= new int [n];// g[0..n-1] are the grades // Read in the grades int i= 0; while (i != g.length) { g[i]= Integer.parseInt(stdin.readLine()); i= i+1; } // Print grades in reverse order int k= n; while (k > 0) { System.out.println(g[k - 1]); k = k-1; } }
14
CS100Lecture 1014 Rewrite previous with for loops int n= Integer.parseInt(stdin.readLine()); // number of grades int[ ] g= new int [n];// g[0..n-1] are the grades // Read in the grades for(int i = 0; i < g.length; i++) { g[i]= Integer.parseInt(stdin.readLine());} // Print grades in reverse order for (int k = n; k > 0; k--) { System.out.println(g[k - 1]); }
15
CS100Lecture 1015 Histogram example -- explanation Program scheme to print “histogram” of grades // Read in a list of grades in the range 0..100, preceded // by the number of grades, and // print out how many times each grade appears int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will be the no. of times grade i appears // Initialize frequencies f[0..100] to 0. // Read in the grades and make up array f. // Print the grades and their frequencies (print only the grades in 0..10 // that appeared at least once in the input) }
16
CS100Lecture 1016 // Read in a list of grades in the range 0..100, preceded by the number // of grades, and print out how many times each grade appears int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will be the no. of times grade i appears // Initialize frequencies f[k] to 0. int k= 0; while (k != f.length) {f[k]= 0; k= k+1;}
17
CS100Lecture 1017 // Read in the grades and make up array f int i= 0; // Inv: i grades have been read in and // each f[k], for 0<=k<=100, contains // the no. of times grade k was read in while (i != f.length) { int grade= Integer.parseInt(stdin.readLine() ); f[grade]= f[grade] + 1; i= i+1; }
18
CS100Lecture 1018 // Print the grades and their frequency (print only the // grades in 0..100 that appeared at least once in the // input) int i= 0; // Inv: the grades 0..i-1 that occurred at least // once have been printed while (i != f.length) { if (f[i] != 0) System.out.println(“grade: ” + i + “ frequency: ” + f[i]); i= i+1; }
19
CS100Lecture 1019 Palindromes A palindrome is a word that reads the same backwards and forwards. –The empty string –A –AA –ABBA –NOON The following are palindromes if blanks and punctuation are ignored –able was I ere I saw elba –a man a plan a canal panama
20
CS100Lecture 1020 Is a given array a palindrome? // Return the value of the statement “array b is a palindrome” static public bool isPalindrome(char[ ] b) { int i= 0; int j= b.length; // Invariant: b is a palindrome iff b[i..j-1] is. In other // words, b[j..length-1] is the reverse of b[0..i-1] while ( j - i >1) { j= j-1; if (b[i] != b[j]) return false; i= i+1; } // {b[i..j-1] has 0 or 1 elements, so it’s a palindrome} return true; }
21
CS100Lecture 1021 Odd Syntax Thing int[] grades; is equivalent to int grades[]; Be careful however: int a, b, c; int[] x, y, z; int r[], s, t[]; Best to associate array brackets with the type not the variable name
22
CS100Lecture 1022 Initializer Lists You can initialize the elements of an array just as you can initialize a variable Example int[] grades = {88, 92, 65, 77, 33}; grades is now an array of 5 integers with values as shown Such an initializer list can only be used when the array is first declared Note curly braces
23
CS100Lecture 1023 Arrays of objects Remember: Java allows arrays of more than ints Suppose you would like an array of strings: String[] words = new String[25]; We could declare an array of employees, as we saw previously: Employee[] workers = new Employee[25]; workers[3] = new Employee(“Millett”, 1999); workers[3].setSalary(50000);
24
CS100Lecture 1024 Arrays as parameters Similar to when passing objects as parameters Declaration: z(int[] list) Call: z(a) where a is declared int[] z can change elements of the array, but not array reference itself Can also pass elements of the array as parameters Declaration: q(int x) Call: q(a[3])
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.