Getting the array length of a 2D array in Java
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.
Java 2D array length
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.
Getting the length of two-dimensional array
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
Get the size of a 2D array
In Java, 2D arrays are really arrays of arrays with possibly different lengths (there are no guarantees that in 2D arrays that the 2nd dimension arrays all be the same length)
You can get the length of any 2nd dimension array as z[n].length
where 0 <= n < z.length
.
If you're treating your 2D array as a matrix, you can simply get z.length
and z[0].length
, but note that you might be making an assumption that for each array in the 2nd dimension that the length is the same (for some programs this might be a reasonable assumption).
How to get rows and columns count of a 2D array in Java?
Well you probably want array_name.length
for getting the count of the rows and array_name[0].length
for the columns. That is, if you defined your array like so:
T[][] array_name = new T[row][col];
array_name.length // row
array_name[0].length // col
How to get length of rows and columns in Two-Dimensional array?
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
Multidimensional Arrays lengths in Java
This will give you the length of the array at index i
pathList[i].length
It's important to note that unlike C or C++, the length of the elements of a two-dimensional array in Java need not be equal. For example, when pathList
is instantiated equal to new int[6][]
, it can hold 6 int []
instances, each of which can be a different length.
So when you create arrays the way you've shown in your question, you may as well do
pathList[0].length
since you know that they all have the same length. In the other cases, you need to define, specific to your application exactly what the length of the second dimension means - it might be the maximum of the lengths all the elements, or perhaps the minimum. In most cases, you'll need to iterate over all elements and read their lengths to make a decision:
for(int i = 0; i < pathList.length; i++)
{
int currLen = pathList[i].length;
}
Related Topics
How Does the JPA @Sequencegenerator Annotation Work
How to Hide Cursor in a Swing Application
How Do Format a Phone Number as a String in Java
Java.Net.Connectexception :Connection Timed Out: Connect
How to Rotate Jpeg Images Based on the Orientation Metadata
Javafx Entirely Customized Windows
Java Se 6 VS. Jre 1.6 VS. Jdk 1.6 - What Do These Mean
Org.Postgresql.Util.Psqlexception: Fatal: Sorry, Too Many Clients Already
Oracle Jdbc Intermittent Connection Issue
Java Date Cut Off Time Information
What Are the Advantages of Using an Executorservice
How to Pause/Sleep/Wait in a Java Swing App
How to Insert an Pdpage Within Another Pdpage with PDFbox
How to Ensure in Java That the Current Local Time Is Correct