cs100lecture 101 announcements assignment p2 is due on thursday assignment p3 is handed out today...
TRANSCRIPT
CS100 Lecture 10 1
Announcements
• Assignment P2 is due on Thursday
• Assignment P3 is handed out today
• Prelim on Monday the 19th. Coming soooooooooon.
CS100 Lecture 10 2
Today’s Topics
• Review
• Arrays– Allocation/Deallocation– Subscripting– Use of arrays to store large collections of data– Several examples
CS100 Lecture 10 3
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
CS100 Lecture 10 4
Hypothetical Problem• Input: zero or more grades from 0 to 100 preceded
by the number of grades5 90 85 40 89 12
• 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?
CS100 Lecture 10 5
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
CS100 Lecture 10 6
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
CS100 Lecture 10 7
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
. . .
93322
CS100 Lecture 10 8
Some notation
segment number of values in it empty when
g[h..i-1] i-h h = i
g[i..j] j+1-i j = i-1
g[j+1..k-1] k-(j+1) j = k
h i j k
g
CS100 Lecture 10 9
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
null
null
CS100 Lecture 10 10
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
CS100 Lecture 10 11
More on declaration and accessing
• What happens in the statemente[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.
CS100 Lecture 10 12
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
CS100 Lecture 10 13
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; } }
CS100 Lecture 10 14
Rewrite previous with for loopsint 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]); }
CS100 Lecture 10 15
Histogram example -- explanationProgram 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)
}
CS100 Lecture 10 16
// 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;}
CS100 Lecture 10 17
// 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;
}
CS100 Lecture 10 18
// 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;
}
CS100 Lecture 10 19
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
CS100 Lecture 10 20
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; }
CS100 Lecture 10 21
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
CS100 Lecture 10 22
Initializer Lists
• You can initialize the elements of an array just as you can initialize a variable
• Exampleint[] 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 curlybraces
CS100 Lecture 10 23
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);
CS100 Lecture 10 24
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 parametersDeclaration: q(int x)Call: q(a[3])