Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

.length on 2d array ?

  Asked By: Hondo    Date: Dec 18    Category: Java    Views: 1999

I was wondering if .length can be used on a 2d array

i.e. of an array a[x][y] i want to do a .length on either the columns or the
rows... so i can do a

for( number of rows ;;) {
for( number of columns;;) {

type iteration through the entire array.



1 Answer Found

Answer #1    Answered By: Julio Morgan     Answered On: Dec 18

Yes there is. The usage is:
int[][] ints = new int[20][20];
for(int i = 0; i < ints.length; i++)
for(int ii = 0; ii < ints[i].length; ii++)
//Do your stuff

The main thing here is that ints[i] returns an int [] and not an int
itself. So the first for-loop is to walk through all the arrays and
the second for-loop is to walk through the array  itself.

Didn't find what you were looking for? Find more on .length on 2d array ? Or get search suggestion and latest updates.