/* * IntArrayEx.java * * Created on September 9, 2002, 12:00 AM * * Purpose is merely to illustrate integer arrays - everything is really in the main */ package simpleEx; /** * * @author mike */ public class IntArrayEx { /** Creates a new instance of IntArrayEx */ public IntArrayEx() { } // static - don't need an object public static void showArray(int arr[]) { int cnt; for (cnt = 0; cnt < arr.length; cnt++) { System.out.println(arr[cnt]); } } /** * @param args the command line arguments */ public static void main(String[] args) { int cnt; // define an array of ints int empty[]; //System.out.println("empty array"); //showArray(empty); // compiler stops //empty[0] = 3; // invalid - array doesn't exist - compiler catches // define an array of 5 ints int vals[] = new int[5]; // uninitialized - all zeros System.out.println("Initial vals array"); showArray(vals); // give the array some values for (cnt = 0; cnt < vals.length; cnt++) { // assign a value vals[cnt] = (cnt + 1) * (cnt + 1); } System.out.println("vals array with some changes"); showArray(vals); // declare an array and create with initializer list int second[] = { 2, 4, 6, 8}; System.out.println("second array"); showArray(second); // get set to actually create the array known as empty int size = 6; // could be set by asking user, finding needed size in a file, or calculations empty = new int[size]; // the elements are set to zero, since primitive type empty[0] = 3; // valid - array doesn't exist - compiler catches System.out.println("revised, not so empty array"); showArray(empty); // try to go outside the bounds - crash ArrayIndexOutOfBounds exception //vals[5] = 42; // try to expand the array - doesn't do what was probably expected // - because entirely new array was created (initialized to zero) // - previous existing elements were blown away //vals = new int[size+1]; //vals[5] = 37; //System.out.println("revised, expanded vals array"); //showArray(vals); // instead must create a new array, copy contents int expanded [] = new int[size+1]; expanded[5] = 37; // copy previous contents System.arraycopy(vals,0,expanded,0,vals.length); System.out.println("expanded array"); showArray(expanded); // make a change - as would be expected, this changes the expanded array // but not the vals array it was copied from - but see TestBankExHand // to see differnet behaviour when working with array of objects expanded[3] = 13; System.out.println("vals[3]: " + vals[3]); System.out.println("expanded[3]: " + expanded[3]); // now, getting back to the original aim - expanding an existing array // presumably you want this back in the same variable so code can go on vals = expanded; // now points to the SAME array // display the arrays System.out.println("expanded vals array (gee it is the same): "); showArray(vals); System.out.println("expanded array: "); showArray(expanded); // make a change - perhaps surprisingly to many, this changes the expanded array // AND the vals array - since both variables really are referencing // the same array expanded[6] = 49; System.out.println("vals[6]: " + vals[6]); System.out.println("expanded[6]: " + expanded[6]); // make a copy - but make it a clone // clone returns an Object - which an array is // but to obtain an array, must use cast // clone can be used when the target array is going to be created now // while arraycopy needs the target array to already exist int myClone[] = (int[]) vals.clone(); System.out.println("clone of expanded vals array: "); showArray(myClone); // make a change - as would be expected, this changes the clone array // but not the vals array it was copied from - but see TestBankExHand // to see different behaviour when working with array of objects myClone[6] = 59; System.out.println("vals[6]: " + vals[6]); System.out.println("myClone[6]: " + myClone[6]); // array of arrays - sort of like multidimensional arrays - but don't have to be square //int table[][] = new int[][] { {0}, {0, 1}, {0, 1, 2}, {0, 1, 2, 3} }; int table[][] = { {0}, {0, 1}, {0, 1, 2}, {0, 1, 2, 3} }; for (cnt = 0; cnt < table.length; cnt++){ System.out.println("==="); showArray(table[cnt]); } } }