CSC297.Java, Class 10: Arrays Admin: * Nametags! * Web site finally fixed. * I come to class sick. I hope you would decide to, too. * Do you prefer "overhead" or "side-by-side"? Overview: * Review: Vectors in Scheme, Arrays in Perl * One-dimensional Arrays in Java * Java Arrays vs. Scheme Vectors * An alternative: Vectors in Java * A note on a future design of Java Review: * What is a Vector in Scheme?: Something like a list * Collection of data. * Fixed-length. * Quick access to individual elements by numbers. * Mutable. * (vector-ref vector-of-students 10) takes only 1 steps (independent of the 10) * (list-ref list-of-students 10) takes about 10 steps /In Java, arrays are similar/ * Collections of data. * Fixed length. * Quick access to individual elements by number. * Mutable. /Syntax/ * Declare TYPE[] NAME; TYPE[] NAME = INITIALIZATION; * Uninitialized (not a good idea; you can't use an unitialized array) int[] grades; * Creating an array of size 10 and naming it grades. grades = new int[10]; * Combining the two int[] grades = new int[10]; * Alternative int[] grades = { 90, 20, 85, 54, 102 }; * What can you do with arrays once you've created them? * Get their length NAME.length * Get an element NAME[INDEX-EXPRESSION] * Set an element NAME[INDEX-EXPRESSION] = VALUE; * For example, to print out all the grades, for (int i = 0; i < grades.length; i++) { screen.println("Grade " + i + " is " + grades[i] + "."); } * See ArrayStuff.java for sample code for changing the grades. * Arrays can store any type, but they must be homogenous * One variant: You can make an array of objects, and then it can store any kind of object in each cell. * Java arrays are (mostly) homogeneous and Scheme vectors are mostly heterogeneous /Why are arrays useful?/ * Convenient way to store and manipulate "lots of stuff" * Somewhat Dynamic: You can create an array at "runtime" and provide as much room as you need. [Wellons] /Java arrays as a type/ * Every array is an object. * The methods look different, but they are still methods. * Get their length * Get a value * Set a value * Designers of Java might have sensibly required you to write grades.length() grades.get(i) grades.set(i,newvalue) * The brackets are "syntactic sugar", as it were. * However, the "make an array of XXX" operation is special. It is a type constructor * Important to be able to make restricted types for type checking. /Java provides an interesting alternative to arrays: Vectors/ * Vectors are collections of values. * Most of the time, getting or setting the ith value takes 1 step. * Getting the length of a vector takes 1 step. * Vectors can grow as you need them to. * Vectors can only contain objects (no primitive types) * Java won't make sure that when you expect a vector of strings, you get a vector of strings /Assignment/ * Write a program that reads a sequence of lines of input and stores them in an array. * Print out "interesting" information about the elements of the array (e.g., shortest line, longest line, alphabetically first line) * Expect to use: * Input procedures * For loops * String operations (e.g., compareTo) * Conditionals * The wonderful "greatest" algorithm you learned in 151.