Presentation is loading. Please wait.

Presentation is loading. Please wait.

One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;

Similar presentations


Presentation on theme: "One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;"— Presentation transcript:

1 One Dimensional Arrays

2 Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;

3 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] ;

4 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”};

5 Looping through 1-D arrays f or (int i = 0; I < theArray.length; i++) System.out.println(theArray[i]); or for( int n : theArray) System.out.println(n); NOTE: for each loops CANNOT be used to modify the contents of arrays for( int n : theArray) n = 0;//NOT ALLOWED

6 Two Dimensional Arrays

7 2-D arrays are matrices 0123 011253146 110111213 2 7 8 910 321222324

8 Declaring references to 2-D array objects int[][] somearray; String[][] crossword; Actor[][] grid;

9 Creating 2-D array objects double[][] matrix = new double[7][10]; String[][] table = new String[10][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 012 0136 14911

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 (int r = 0; r < mat.length; r++) for(int c = 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)

12 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(int arr[] : mat) for(int value : arr) System.out.println(value);

13 Some practice in a BlueJ test class 1.Create a 5 by 7 2-D Arrays of ints and loop through it and fill with the number 3. 2.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). 3.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) 4.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.

14 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]);


Download ppt "One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;"

Similar presentations


Ads by Google