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.
2d Array length rows and column length (Java) - Stack Overflow
java - 2D array row and column length - Stack Overflow
java - Getting the length of two-dimensional array - Stack Overflow
How to get rows and columns count of a 2D array in Java? - Stack Overflow
Videos
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.
which 3?
You've created a multi-dimentional array. nir is an array of int arrays; you've got two arrays of length three.
System.out.println(nir[0].length);
would give you the length of your first array.
Also worth noting is that you don't have to initialize a multi-dimensional array as you did, which means all the arrays don't have to be the same length (or exist at all).
int nir[][] = new int[5][];
nir[0] = new int[5];
nir[1] = new int[3];
System.out.println(nir[0].length); // 5
System.out.println(nir[1].length); // 3
System.out.println(nir[2].length); // Null pointer exception
In the latest version of JAVA this is how you do it:
nir.length //is the first dimension
nir[0].length //is the second dimension
The way you should look at it is that you actually make an array of an array of ints. For example you could initialize it like this:
int[][] array = {
{1, 2, 3, 4}, // This is the first array in the multidimensional array
{5, 6, 7, 8}, // This is the second array in the multidimensional array
{9, 10, 11, 12} // This is the third array in the multidimensional array
};
Note that each of these individual arrays inside the multidimensional array has length 4.
So when you ask the length like this:
array.length // = 3
what Java does is give you the number of int[] in the array. Now every array in this array has length 4, hence:
array[0].length // = 4
Also let's say you wanted to access the element with value 7. This element can be found in the second array on the third place. Since Java indexing starts at 0, you can access that element as follows.
array[1][2] // value of this element is 7
You can read more here https://www.programiz.com/java-programming/multidimensional-array. Multidimensional arrays are hard in the beginning, but once you get them, you will use them often.
you can think of the following array variable as array of arrays, it consists of 3 arrays each of which contain 4 elements
int [][]array = new int[3][4];
so when you try array.length you get 3 the first array that contains 3 arrays which contain 4 elements each, however when you array[0].length,array[1].length, array[2].length, you are checking lengths of arrays of elements in array variable, which all give you 4.
Edit:
lets say you want to take out the arrays from array variable array and check lengths
int [] arr1 = array[0]; // arr1.length = array[0].length which is 4
int [] arr2 = array[1]; // arr2.length = array[1].length which is 4
int [] arr3 = array[2]; //arr3.length = array[2].length which is 4