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
Answer from Brian Roach on Stack Overflowwhich 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
Videos
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.
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.
So as we know 2D arrays have to indices. I want to change the second index. So the first indices point to knew indices. For example: a points to 1, 2,3. But i want to change it to 4,5,6.
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
ETA: Here is an excerpt from the book I'm using to review Java:
...Suppose that x = new int[3][4], x[0], x[1], and x[2] are one-dimensional arrays and each contains four elements...x.length is 3 and x[0].length, x[1].length, and x[2].length are 4.
--
Given that the length of a one-dimensional array is the number of elements, doesn't it make more intuitive sense to apply the same concept to two-dimensional arrays?