ARRAYS ====== An array is an object consisting of a numbered list of variables, each of which is a primitive type or a reference to another object. The variables in an array are always indexed from zero in increments of one. For example, here is an array of characters. 0 1 2 3 --- ----------------- |.+----->| b | l | u | e | --- ----------------- c Like any object, an array is only useful if we can reference it, usually through some reference variable like "c" above. We declare c thusly: char[] c; // Reference to an array (of any length) of characters. We can construct an array of four characters as follows. c = new char[4]; Now that we have an array object, we may fill in its values by indexing c. c[0] = 'b'; // Store the character 'b' at index 0. c[1] = 'l'; c[2] = 'u'; c[3] = 'e'; The characters in a four-element array are indexed from 0 to 3. If we try to address any index outside this range, we will trigger a run-time error. c[4] = 's'; // Program stops with ArrayIndexOutOfBoundsException A _run-time_error_ is an error that doesn't show up when you compile the code, but does show up later when you run the program and the Java Virtual Machine tries to access the out-of-range index. When c references an array, you can find out its length by looking at the field "c.length". You can never assign a value to the "length" field, though. Java will give you a compile-time error if you try. Multi-Dimensional Arrays ------------------------ A _two-dimensional_array_ is an array of references to arrays. A three- dimensional array is an array of arrays of arrays. As an example, consider Pascal's Triangle. 1 <-- row 0 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 <-- row 5 Each entry is the sum of the two nearest entries in the row immediately above. If the rows are numbered from zero, row i represents the coefficients of the polynomial (x + 1)^i. For example, (x + 1)^4 = x^4 + 4x^3 + 6x^2 + 4x + 1. The following method returns an array of arrays of ints that stores the first n rows of Pascal's Triangle. public static int[][] pascalTriangle(int n) { int[][] pt = new int[n][]; Here, we've just declared pt to reference an array of arrays, and constructed an array for it to reference. However, the arrays that this array will reference do not yet exist. They are constructed and filled in by the following loop. for (int i = 0; i < n; i++) { pt[i] = new int[i + 1]; // Construct row i. pt[i][0] = 1; // Leftmost value of row i. for (int j = 1; j < i; j++) { pt[i][j] = pt[i - 1][j - 1] + pt[i - 1][j]; // Sum 2 entries above. } pt[i][i] = 1; // Rightmost value of row i. } return pt; } Our array objects look like this: ----- ------------------------------->| 1 | | ----------- | ------------------------->| 1 | 1 | | | ----------------- | | ------------------->| 1 | 2 | 1 | | | | ----------------------- | | | ------------->| 1 | 3 | 3 | 1 | --- ---+-----+-----+-----+-------- ----------------------------- pt |.+----->| . | . | . | . | .-+---->| 1 | 4 | 6 | 4 | 1 | --- ------------------------------ ----------------------------- MORE ARRAYS =========== Automatic Array Construction ---------------------------- Last lecture, we used a loop to construct all the arrays that the top-level array references. This was necessary to construct a triangular array. But if you want a rectangular multi-dimensional array, rather than a triangular one, Java can construct all of the arrays for you at once. int[][] table = new int[x][y]; This declaration constructs an array of x references to arrays. It also constructs x arrays of y ints. The variable "table" references the array of arrays; and each entry in the array of arrays references one of the arrays of ints. All the arrays are constructed for you at once. Similarly, Java can construct three- or ten-dimensional arrays for you, memory permitting. We could have used a square array to store Pascal's Triangle, but that would have unnecessarily wasted memory. If you have enough memory, you might not care. When you declare a variable, you can also construct array entries by using initializers. Human[] b = {amanda, rishi, new Human("Paolo")}; int[][] c = {{7, 3, 2}, {x}, {8, 5, 0, 0}, {y + z, 3}}; In the second example, Java constructs a non-rectangular two-dimensional array, composed of one array of arrays and four arrays of ints. Outside of declarations, you need a more complicated notation. d = new int[] {3, 7}; f(new int[] {1, 2, 3}); Another subtlety of array declarations is the following. int[] a, b, c; // a, b, and c all reference arrays. int a[], b, c[][]; // a is 1D; c is 2D; b is not a reference/array. int[] a, b[]; // a references a 1D array; b references a 2D array. Arrays of Objects ----------------- When you construct a multi-dimensional array, Java can construct all the arrays for you. But when you construct an array of objects, Java does not construct the objects automatically. The array contains space for references to the objects. You must construct the objects yourself. String[] sentence = new String[3]; sentence[0] = "Word"; sentence[2] = new String(); --- -------------------- --- sentence |.+----->| . | null | .--+---->| | --- ---+---------------- --- empty String | | -------- \---->| Word | -------- main()'s Parameter ------------------ What is the array of Strings that the main() method takes as a parameter? It's a list of command-line arguments sent to your Java program, prepared for you by Java. Consider the following program. class Echo { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } } } If we compile this and type "java Echo kneel and worship Java", java prints kneel --- --------------------------------- and args |.+---->| . | . | . | . | worship --- ----+-------+-------+-------+---- Java | | | | v v v v ------- ----- --------- ------ |kneel| |and| |worship| |Java| ------- ----- --------- ------