// DrJava saved history v2 // arrays again //first declare the array reference variable int dailySales[]; // now you MUST CREATE the array object dailySales = new int[7]; // or you can declare the array and assign values in one move // This works only with primitives. int dailyPurchase [] = {59, 32,87,34,46,75,25}; // accessing an array using a for loop - notice that // every array has a length field for (int i = 0; i < dailyPurchase.length; i++) { System.out.println(dailyPurchase[i]); } // 2-dimensional arrays (basically an array of arrays) int sales [] [] = new int [3] [4]; // assign a value to row 2 column 2 sales [1] [1] = 43 sales [1] [1] // use a loop to assign values using an algorithm // notice the nested for-loop required for 2-d arrays. for (int i = 0; i < sales.length; i++) { for (int j = 0; j < sales[i].length; j++) { sales[i][j] = (i+j)* (i+1); } } // to access the items in the array - we use a nested for-loop for (int i = 0; i < sales.length; i++) { for (int j = 0; j < sales[i].length; j++) { System.out.println(i + ", " + j + ", " +sales[i][j]); } }