0% found this document useful (0 votes)
3 views

Java 2d Arrays

Uploaded by

Queenzlet Turao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java 2d Arrays

Uploaded by

Queenzlet Turao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1

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

Match the Array


0 1
1 0
0 1
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 We have only worked with one-dimensional arrays so far, which


0
0 have a single row of elements.
1
0 1
1 0
0 But in the real world, data is often represented in a two- 1
0
1
0 dimensional table with rows and columns. 0
0 1
1 0
0 0
1 Programming languages can also represent arrays this way with 1
0 0
1 multiple dimensions. 1
0 1
0
1
1
1
2D Arrays
A two-dimensional (2D) array has rows and columns.

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,

1 type[][] name = new type[row][col];


0
0
1
0
where row, col is the number of rows/columns. When arrays are created their 1
1 contents are automatically initialized to 0 for numeric types, null for object 0
0 1
1
references, and false for type boolean. 0
0 0
0 1
1 int[][] matrix = new int[3][4]; //3 rows, 4 columns 0
0
1
//all initialized to 0. 0
1
0 0
1 1
0 1
0
1
1
1
2D Array
To explicitly put a value in an array, you can use assignment
statements specifying the row and column of the entry.
1
0
0
1
int[][] matrix = new int[3][4]; //3 rows, 4 columns
0
1
//all initialized to 0. 1
0
0 matrix[0][0] = 2; 1
0
1
0 matrix[1][2] = -6; 0
0 1
1 matrix[2][1] = 7; 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Initializer List
You can also initialize (set) the values for the array when you create it. In this case
you don’t need to specify the size of the array, it will be determined from the values
1
0 you give. This is called using initializer list.
0
1
0 1
1
int[] array = {1, 4, 3}; // 1D array initializer list. 0
0 // 2D array initializer list. 1
1 0
0 int[][] mat = {{3, 4, 5}, {6, 7, 8}}; 0
0 1
1 // 2 rows, 3 columns 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Declare and Initialize
Declaring and initializing 2D arrays.
int[][] table; //2D array of ints, null reference
1
0
0 double[][] matrix=new double[4][5];
1
0 // 4 rows, 5 columns 1
1 0
0 // initialized all to 0.0 1
1 0
0 0
0 1
1
String[][] strs=new String[2][5]; 0
0 // strs reference 2x5 array of String objects. Each element is null. 0
1 1
0 0
1 1
0 // Using initializer list. 1
0
String[][] seatingInfo = {{"Jamal", "Maria"}, 1
1
{"Jake", "Suzy"}, {"Emma", "Luke"}}; 1
ACCESS ARRAY
int[][] a = {{5, 10, 15}, {20, 25, 30}, {35, 40, 45}};
1
0
0
1 5 10 15
0 1
1
System.out.println(a[1][1]); 0
0 1
1 0
0
0
20 25 30 0
1
1 System.out.println(a[2][0]); 0
0 0
1 1
0
1
35 40 45 0
1
0 1
0
1
1
1
ACCESS 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

You might also like