/** * Instantiate an array and fill it */ public class InstantiateFillPrintArray { public static void main(String [] args) { int x = 10; System.out.println("x = " + x); // declare the array; creating a space to hold its memory address int [] xArray; // create (instantiate) the array xArray = new int[4]; // This loop fills the array with powers of 2 for (int index = 0; index < xArray.length; index++) { xArray[index] = (int)(Math.pow(2,index)); } // This doesn't work! (Prints memory address, not contents) System.out.println("xArray = " + xArray); // Do this instead: for (int i = 0; i < xArray.length; i++) System.out.println("array [" + i + "] = " + xArray[i]); } }