Consider
public static void main(String[] args) {
int[][] foo = new int[][] {
new int[] { 1, 2, 3 },
new int[] { 1, 2, 3, 4},
};
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
}
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
Answer from NG. on Stack OverflowConsider
public static void main(String[] args) {
int[][] foo = new int[][] {
new int[] { 1, 2, 3 },
new int[] { 1, 2, 3, 4},
};
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
}
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
int[][] test;
test = new int[5][];//'2D array'
for (int i=0;i<test.length;i++)
test[i] = new int[i];
System.out.println(Arrays.deepToString(test));
Object[] test2;
test2 = new Object[5];//array of objects
for (int i=0;i<test2.length;i++)
test2[i] = new int[i];//array is a object too
System.out.println(Arrays.deepToString(test2));
}
}
Outputs
[[], [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
[[], [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
The arrays test and test2 are (more or less) the same.
java - How to get length of rows and columns in Two-Dimensional array? - Stack Overflow
How to get rows and columns count of a 2D array in Java? - Stack Overflow
2d Array length rows and column length (Java) - Stack Overflow
java - 2D array row and column length - Stack Overflow
In order to better understand this, take a look at this image:

This image is what you call 2D array, as you can see, it's actually an array of arrays.
nums.length will return the length of the blue array (which is the number of the rows).
Now if you want to get the number of columns, you should access one row by nums[0] for example, and then do nums[0].length, which will yield 4.
Now, simply replace nums with array...
Note: As you see in the image, the number of columns might differ and it doesn't have to be the same for each row.
It's important to understand that Java doesn't really have two-dimensional arrays. It has arrays of arrays. That means, for instance, that you can have this:
int[][] array=
{
{1},
{1, 2, 3},
{1, 2, 3, 4, 5},
{1, 2}
};
So there is no one upper bound of the second level. Java arrays are inherently jagged, each of the second level in the above has its own length.
So to loop them correctly, you have to check for each of the second-level arrays:
int x, y;
int[] second;
for (x = 0; x < array.length; ++x) {
second = array[x];
for (y = 0; y < second.length; ++y) {
// ....
}
}
Full example: Live Copy
public class ArrayExample {
public static void main(String[] args) {
int[][] array=
{
{1},
{1, 2, 3},
{1, 2, 3, 4, 5},
{1, 2}
};
int x, y;
int[] second;
for (x = 0; x < array.length; ++x) {
second = array[x];
for (y = 0; y < second.length; ++y) {
System.out.println(x + "," + y + ": " + second[y]);
}
System.out.println();
}
}
}
Output:
0,0: 1 1,0: 1 1,1: 2 1,2: 3 2,0: 1 2,1: 2 2,2: 3 2,3: 4 2,4: 5 3,0: 1 3,1: 2
Or if you don't need the indexes, just the values, you can use the enhanced for loop: Live Example
public class ArrayExample {
public static void main(String[] args) {
int[][] array=
{
{1},
{1, 2, 3},
{1, 2, 3, 4, 5},
{1, 2}
};
for (int[] second : array) {
for (int entry : second) {
System.out.println(entry);
}
System.out.println();
}
}
}
Output:
1 1 2 3 1 2 3 4 5 1 2
Think of it like this: for a 2-D array such as int[][] example, the number of rows is example.length, while the number of columns in a row, eg. example[0], is the length of that particular row, which can be expressed as example[0].length.
Note: the number of columns in each row can be different. For example, you could define example as:
int[][] example = { {1, 2}, // row of length 2
{2, 3, 4}, // row of length 3
{6} }; // row of length 1
Java technically doesn't have 2-dimensional arrays, it has arrays of arrays.
System.out.println(example.length); // number of rows
System.out.println(example[0].length); // number of columns in first row
But This could fail if:
- array size is 0, in which you will get an exception.
- You are explicitly assuming that size of first element of array as size of rows, this could be ambiguous if you are taking a Jagged array.
Jagged Array: Jagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D arrays but with variable number of columns in each row.
This line gives the size of each row.
You know that
- a[0]={1, 2, 3}
- a[1]={1, 2, 3}
- a[2]={1, 2, 3}
So, a[0].length = a[1].length = a[2].length = 3. Use of this is to ensure that we dont go Out Of Array Bounds.
Java doesn't have 2D arrays. Java has arrays of arrays. The second loop uses column < a[row].length to make sure that you don't iterate past the length of the row-th array. You need this to handle nested arrays of varying length.
Edit: solved. It is the same as Java as someone pointed out
In java it would be
grid.length for number of rows and grid[i].length for number of columns (the code is in a nested for loop)
If it's guaranteed that each row has the same length, just use:
int rows = matrix.length;
int cols = matrix[0].length; // assuming rows >= 1
(In mathematics this is of course guaranteed, but it's quite possible in most languages to have an array of arrays, where the inner arrays are not all the same length).
int row = mat.length;
int col= mat[0].length;
Mostly in array all row has same length. So above solution will work almost every time.