Java 2d Arrays
Java 2d Arrays
0
1
LET’S 0
0
1
0
PLAY A
1
1 0
0 1
0 0
1 0
GAME!
0 1
1 0
0 1
1 0
0
1
INSTRUCTION SPECIALITS:
0 CARL DREDD ROSALES
1
0
1
0
0
1
0
1
0
0
1
0
1
1 0
0 1
0 0
1 0
by Columns
0
1
0
1
0
1
0
0
1
0
1
0
0
1
0
1
1 0
0
2D ARRAYS
1
0 0
1 0
0 1
1 0
0 1
1 0
0
1
INSTRUCTION SPECIALITS:
0 CARL DREDD ROSALES
1
0
1
0
0
2D Arrays
1
0
A row has horizontal elements. A column has vertical
0 elements. In the picture below there are 3 rows of lockers and
1
0 6 columns. 1
1 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
2D Arrays
Two dimensional arrays are especially useful when the
1
0
data is naturally organized in rows and columns like in a
0 spreadsheet, bingo, battleship, theater seats, classroom
1
0 seats, connect-four game, or a picture. 1
0
1
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
2D Arrays
● A one-dimensional array stores a list of elements
1
● A two-dimensional array can be thought of as a table of
0
0
elements, with rows and columns
1
0 1
1 0
1
0
1 one two 0
0
0
0 dimension dimensions 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
2D Arrays
Many programming languages actually store two-dimensional array data in a one-
dimensional array. The typical way to do this is to store all the data for the first
1 row followed by all the data for the second row and so on. This is called row-
0
0
major order.
1 Some languages store all the data for the first column followed by all the data for 1
0
1 the second column and so on. This called column-major order. 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Declare and Initialize
To declare and initialize a 2D array,
1
0 type[][] name = new type[row][col];
0
1
0 1
1
0
where row, col is the number of rows/columns. When arrays are 0
1
1 created their contents are automatically initialized to 0 for numeric 0
0 0
0 types, null for object references, and false for type boolean. 1
1 0
0 0
1 1
0 int[][] matrix = new int[3][4]; //3 rows, 4 columns 0
1 1
0 //all initialized to 1
0
0. 1
1
1
Declare and Initialize
To declare and initialize a 2D array,
Example
1
0
0
int[ ][ ] myNumbers = { {1, 2, 3, 4}, {5, 6, 7}};
1
0 System.out.println(myNumbers[1][2]); 1
1 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Array of Arrays
A 2D array is implemented as an array of row arrays. Each row is a one-dimensional
array of elements. Suppose that mat is the 2D array:
1
0
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 Then mat is an array of three arrays: 0
0 0
1
mat[0] is the one-dimensional array {3,-4,1,2}. 1
0 0
1
mat[1] is the one-dimensional array {6,0,8,1}. 1
0 mat[2] is the one-dimensional array {-2,9,1,7}. 1
0
mat.length is the number of rows. 1
1
1
Array of Arrays
1
0
0
1
0 1
1
1) mat.length is the number of rows. In this case, it equals 3 because there 0
0 are three row-arrays in mat. 1
1 0
0 0
0 1
1 2) For each k, where 0 <=k <mat.length, mat[k].length is the number of 0
0 0
1
elements in that row, namely the number of columns. In this case, 1
0 mat[k].length=4 for all k. 0
1 1
0 1
0
3) Java allows “jagged arrays” where each row array may have different 1
1
lengths. 1
Example
int[][] mat={{3,4,5},{1,2},{0,1,-3,5}};
1
0
0 mat[0] = {3,4,5}
1
0
1
mat[1] = {1,2} 1
0
0 1
1 mat[2] = {0,1,-3,5} 0
0 0
0 1
1 0
0
1
mat.length = 3 0
1
0 0
1 mat[0].length = 3 1
0 1
mat[1].length = 2 0
1
mat[2].length = 4 1
1
Row Major Order
Suppose that mat is a 2D array initialized with integers. Use nested for loop to print out
The elements of the array. Traverse by row-major order.
1
0 int[][] mat = {{3,4,5},{1,2},{0,1,-3,5}};
0
1 for (int row = 0; row < mat.length; row++) {
0 1
1 for (int col = 0; col < mat[row].length; col++) { 0
0 1
1 System.out.print(mat[row][col] + " "); 0
0 0
0 } 1
1 0
0 System.out.println(); 0
1 1
0
} Output: 0
1 1
0 345 1
12 0
1
0 1 -3 5 1
1
Column Major Order
Suppose that mat is a 2D array initialized with integers. Use nested for loop to print
out the elements of the array. Traverse by column-major order. Assume that the
array is rectangular.
1
0 int[][] mat = {{3,4,5},{1,2,3}};
0
1 for (int col = 0; col < mat[0].length; col++) {
0 1
1 for (int row = 0; row < mat.length; row++) { 0
0 1
1 System.out.print(mat[row][col] + " "); 0
0 0
0 } 1
1 0
0 System.out.println(); 0
1 1
0
} 0
1
Output: 1
0 31 1
0
42 1
53 1
1
JAGGED ARRAY
A jagged Array is an array of arrays where each element is an
array. It is a special type of Multidimensional array where there
1
0 are a variable number of columns in each row.
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
JAGGED ARRAY
int[ ][ ] jaggedArray = new int[3][ ];
1 jaggedArray[0] = new int[]{1, 2, 3};
0
0 jaggedArray[1] = new int[]{4, 5};
1
0 jaggedArray[2] = new int[]{6, 7, 8, 9}; 1
1 0
0 1
1
for (int i = 0; i < jaggedArray.length; i++) { 0
0 0
0
for (int j = 0; j < jaggedArray[i].length; j++) { 1
1 0
0 System.out.print(jaggedArray[i][j] + " "); 0
1 1
0 } 0
1 1
0 System.out.println(); 1
0
} 1
1
1
For Each Traversal
Traverse an array by using a for each loop. For each loop, in general, are much easier to work with.
If you are not modifying your 2D array, it is highly recommended that you use for each to avoid
index
1
0 errors.
0 For Each Traversal
1
0 1
1 0
0 int[][] mat = {{3,4,5},{1,2,7},{0,1,5}}; 1
1 0
0 for(int row []: mat){ 0
0 1
1 for(int element: row) 0
0 0
1 System.out.println(element + " "); 1
0 Output: 0
1 System.out.println(); 1
0 345 1
} 127 0
1
015 1
1
1
0
0
1
0 1
1 0
0 1
THANK YOU!
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1