Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Finding the length of a two dimensional array

1. ## 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!

2. ## 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.

3. ## Re: Finding the length of a two dimensional array

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