Finding the length of a two dimensional array

What if I were to use ".length" on a 2d array. What value would it return? Would it return the length of the row, or the length of the column? And for whatever value it would return, how would I make it return the other?

Hope that wasn't a confusing sentance!

Re: Finding the length of a two dimensional array

Ok, well a 2d array is an array of arrays.

So, when you do a .length call on it, it will return the number of arrays inside of it. Just like when you do a .length call on a 1d array of doubles, it will return the number of doubles.

If you wanted to return the length of the arrays in the array, it is a little different. The reason it is different is, in theory, it is possible for each array in the array to have a different length. If we are assuming that all of the arrays in the array have the same length, then you would want to say: **array[0].length**. The reason we say that is because we get the first array with the call **array[0]** and we get the length of that array by applying the **.length** call on the first array.

Tell me if that is confusing.

Re: Finding the length of a two dimensional array

Quote:

Originally Posted by

**aussiemcgr**
Ok, well a 2d array is an array of arrays.

So, when you do a .length call on it, it will return the number of arrays inside of it. Just like when you do a .length call on a 1d array of doubles, it will return the number of doubles.

If you wanted to return the length of the arrays in the array, it is a little different. The reason it is different is, in theory, it is possible for each array in the array to have a different length. If we are assuming that all of the arrays in the array have the same length, then you would want to say: **array[0].length**. The reason we say that is because we get the first array with the call **array[0]** and we get the length of that array by applying the **.length** call on the first array.

Tell me if that is confusing.

nope thats fine, thanks for the explaination