[go: up one dir, main page]

0% found this document useful (0 votes)
60 views2 pages

Accessing 2 Dimensional Arrays

The document discusses 2D arrays in Java. It explains that 2D arrays are arrays of arrays, with each subarray representing a row. It demonstrates how to initialize and access a 2D array, print its dimensions, and iterate through its elements. The key points are that Java uses a row-major ordering, the length of the outer array gives the number of rows, and the length of the first subarray gives the number of columns. 2D arrays do not need to be rectangular and subarrays can have different lengths.

Uploaded by

aasxdas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views2 pages

Accessing 2 Dimensional Arrays

The document discusses 2D arrays in Java. It explains that 2D arrays are arrays of arrays, with each subarray representing a row. It demonstrates how to initialize and access a 2D array, print its dimensions, and iterate through its elements. The key points are that Java uses a row-major ordering, the length of the outer array gives the number of rows, and the length of the first subarray gives the number of columns. 2D arrays do not need to be rectangular and subarrays can have different lengths.

Uploaded by

aasxdas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Accessing 2 Dimensional Arrays

public static void main(String args[]){


int [][] matrix = new int[][]{
{1,2,3,4},
{5,6,7,8},
{9,0,1,2},
{3,4,5,6}
};
System.out.println("length is " + matrix.length);

double[][] array = {{1,2,3,3,3,3},{3,4,5,8,9},{8,9,1}};

So 2D arrays are really just arrays of arrays. So if I have a 2D array with 5 rows and 4
columns, you could think of it as an array containing 5 subarrays that have 4 spots each.

boolean landscape[][] = new boolean[5][4];


int numRows = landscape.length;
int numCols = landscape[0].length;
System.out.println("Number of rows: " + numRows);
System.out.println("Number of cols: " + numCols);

for (int i = 0; i < numRows; i++) {


for (int j = 0; j < numCols; j++) {
System.out.print(landscape[i][j]);
}
System.out.println("");
}
Output:

Number of rows: 5
Number of cols: 4
0000
0000
0000
0000
0000

It is important to note that java is considered row major (the row always comes first):

Firstly, Java technically doesn't have 2-dimensional arrays: it has arrays of arrays. So in
Java you can do this:
String arr[][] = new String[] {
new String[3],
new String[4],
new String[5]
};
int rows = arr.length;

There is no requirement that a 2-D array is actually rectangular, or even that all elements in
the first dimension are populated.

If you want to find the number of elements in the first dimension, the answer is
simply array.length.

If you want to find the number of elements in the second dimension of a rectangular
2-D array, the answer is `array[0].length.

You might also like