150 likes | 326 Views
One Dimensional Arrays. Declaring references to array objects. How would you declare a variable somearray that is an array of ints ? int [] somearray ;. Creating array objects. How do you create an array of 12 BankAccount objects and refer to it with the variable theBank ?
E N D
Declaring references to array objects • How would you declare a variable somearray that is an array of ints? • int[] somearray;
Creating array objects • How do you create an array of 12 BankAccount objects and refer to it with the variable theBank? BankAccount[] theBank = new BankAccount[12] ;
Creating 1-D array using initializer list • This shortcut only works for local variables NOT fields. • It is very usual to fill an array with data without having to loop. int[] nums = {1,3,6,4,9,11}; String arr = {“apple” , ”orange” , “pear”};
Looping through 1-D arrays for (inti = 0; I < theArray.length; i++) System.out.println(theArray[i]); or for( intn : theArray) System.out.println(n); NOTE: for each loops CANNOT be used to modify the contents of arrays for( intn : theArray) n = 0; //NOT ALLOWED
2-D arrays are matrices 0 1 2 3 0 11 25 31 46 1 10 11 12 13 • 7 8 9 10 3 21 22 23 24
Declaring references to 2-D array objects • int[][] somearray; • String[][] crossword; • Actor[][] grid;
Creating 2-D array objects • double[][] matrix = new double[7][10]; • String[][] table = new String[10][10];
Creating a 2-D array using an initializer list • This shortcut only works for local variables NOT fields. • Very useful in test classes to get data into an array without looping int[][] nums ={ {1,3,6} , {4,9,11} }; Creates a 2D array as follows 0 1 2 0 1 3 6 1 4 9 11
Loop through a 2-D array Assume a 2-D array of ints called mat has already been created and filled with values – for (intr = 0; r < mat.length; r++) for(intc = 0; c < mat[0].length; c++) System.out.print(mat[r][c]); This moves left to right by row and goes down each column from top to bottom. Row major order – like reading a book. This is the preferred way to loop through a 2-D array in java. Use mat[r] for an array with rows of different lengths (not rectangular array)
2D array is an Array of Array Objects Consider looping through a 2D array using for each loops: Assume a 2-D array of ints called mat has already been created and filled with values – for(intarr[] : mat) for(int value : arr) System.out.println(value);
Some practice in a BlueJ test class • Create a 5 by 7 2-D Arrays of ints and loop through it and fill with the number 3. • Create an 3 by 4 2-D array of String objects and fill it using an initializer list. Loop through and print them in row major order (as you read a book). • Using the same 2-D array of String objects from #2, loop through and print them in column major order (top to bottom THEN left to right) • Create a 10 by 10 2-D array of anything and and fill it using an initializer list. Write 2 separate loops to visit each element of the diagonals and print them.
Most important info. for 2 Arrays Creating a 2D array: int[][] array = new int[10][20]; Row major order means [rows] then [cols] Remember RC cola! Rows and cols numbered starting at 0! To get num rows: arrays.length To get num cols: arrays[0].length (rectangular 2D array) To loop through in row major order for (int r = 0; r < mat.length; r++) for(int c = 0; c < mat[r].length; c++) System.out.print(mat[r][c]);