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;
}
Answer from no.good.at.coding on Stack OverflowThis 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;
}
This is for a 3 dimensional array.
int x[][][]=new int[5][8][10];
System.out.println(x.length+" "+x[1].length+" "+x[0][1].length);
OUTPUT : 5 8 10
Multidimensional Array Length - JavaScript
java - Getting the length of two-dimensional array - Stack Overflow
Finding the size of multi dimensional array?
Javascript multidimensional array length problem - Stack Overflow
Videos
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
An array is a sorted set of numeric key value pairs. "_u" + user_id is not numeric, it's a string, therefore it gets stored as a regular property on the array (it behaves like an object) and not as part of the array itself. If you want to use a key-value storage with a length, use a Map.
const test = { // no need for a class if you dont have instances
arr: new Map(), // no need for that unneccessary init
add() {
let user_id = Math.floor(Math.random() * 10000);
if(!this.arr.has("u_" + user_id)) { // I prefer "this" over "test", both work however
this.arr.set("u_" + user_id, []);
}
this.arr.get("u_" + user_id).push({"somedata": "here"});
},
length() {
return this.arr.size; //Note: it is "size" not "length" on a Map
},
};
Sidenote: arr and test are very bad names.
An array index can be defined as number only. If you want to get the length of an array, there are two ways to achieve this.
- You need to define the index as number instead of a string.
Make a separate object, add your object (
{"somedata": "here"}) into the object and push it into the array. Check the code below.let test=[] let obj = {} let user_id = Math.floor(Math.random() * 10000); if(obj["u_" + user_id] === undefined) { obj["u_" + user_id] = []; } obj["u_" + user_id] = {"somedata": "here"}; test.push(obj)
Hope this will be helpful.