# length function for 2D Arrays

Page 1 of 1

## 9 Replies - 900 Views - Last Post: 03 December 2012 - 04:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302396&amp;s=866243bae2d6205e94a0ff5b9e3655c8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 9
• Posts: 197
• Joined: 21-May 12

# length function for 2D Arrays

Posted 01 December 2012 - 11:47 PM

Say I have an array:

private GamePiece[][] board = new GamePiece[4][5];

How would I get the length of the row or column? If I wrote: board.length in a for loop, which length is it gonna give me, 4 or 5?
Is This A Good Question/Topic? 0

## Replies To: length function for 2D Arrays

### #2 KevinChristian

Reputation: 0
• Posts: 6
• Joined: 18-October 12

## Re: length function for 2D Arrays

Posted 01 December 2012 - 11:52 PM

toad87, on 01 December 2012 - 11:47 PM, said:

Say I have an array:

private GamePiece[][] board = new GamePiece[4][5];

How would I get the length of the row or column? If I wrote: board.length in a for loop, which length is it gonna give me, 4 or 5?

That will give you 4. If you want to find out the length of the second dimension you would have to do board[row].length where row is an integer. board[0].length, or any number from 0-3 would give you an answer of 5. This is done because a ragged array can have different length for their second dimension.

Reputation: 9
• Posts: 197
• Joined: 21-May 12

## Re: length function for 2D Arrays

Posted 01 December 2012 - 11:55 PM

But why is it only 0-3? Are those numbers special?

### #4 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: length function for 2D Arrays

Posted 02 December 2012 - 03:02 AM

toad87, on 02 December 2012 - 01:55 AM, said:

But why is it only 0-3? Are those numbers special?

0, 1, 2, 3 are the allowed indices of a 4-element array which was how the array was defined in your original question:

private GamePiece[][] board = new GamePiece[4][5];

board has 4 rows, 0 - 3, and 5 columns, 0 - 4, for a total of 4 x 5 = 20 possible elements.

Reputation: 9
• Posts: 197
• Joined: 21-May 12

## Re: length function for 2D Arrays

Posted 02 December 2012 - 03:05 AM

Oh I see. So if I had an array of [20][5], i could get the length of the second column by doing array.length[0-19]?

It doesn't matter which number I choose?

### #6 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: length function for 2D Arrays

Posted 02 December 2012 - 03:31 AM

All of the numbers 0 - 19 are valid, but the result could be different depending on which is chosen. In Java, arrays don't have to be perfectly rectangular. array[5].length could be different than array[12].length.

Those pressing this point like to emphasize that in Java, multidimension arrays are really arrays of arrays. For example, you may want to store Strings as character arrays in a 2D array. The Strings can be of different length:

string1 = "I love my country"
string2 = "Bill"
string3 = "Some chemical formula that's miles long"

Then for the character array

char[][] arrayOfStrings = new char[3][];

with

arrayOfStrings[0] = string1.toCharArray();
arrayOfStrings[1] = string2.toCharArray();
arrayOfStrings[2] = string3.toCharArray();

each of the following

arrayOfStrings[0].length
arrayOfStrings[1].length
arrayOfStrings[2].length

will give a different result.

Reputation: 9
• Posts: 197
• Joined: 21-May 12

## Re: length function for 2D Arrays

Posted 02 December 2012 - 03:43 AM

That's a bit confusing for me...

So a 2D array can hold two different type of elements?

In your example...by putting 3 in the first column, you're saying that the second column will have 3 elements of chars. So if you put array[0].length you are getting the length of the first char in he second column.

I kiiiind of get it...but it still feels a bit foggy in my mind.

Thanks for the help though. I really appreciate it.

If you can comment on my code below though.

```for(int i=0; i < board.length; i++){
if(i<9){
System.out.print(" ");
System.out.print(i + 1 + " ");
}else{
System.out.print(i + 1 + " ");
}

for(int j=0; j < board[0].length; j++){

if(board[i][j] == null){
System.out.print(EMPTY + " ");
}else{
System.out.print(board[i][j].getDisplayString() + " ");
}
}
System.out.println();
}

```

So in this nested loop, I put "for(int j=0; j < board[0].length; j++)". It works fine. I found that if I used a higher number, I may run into an arrayindexoutofbounds exception when I run it.

In this case I'm correct for using a 0?

This post has been edited by toad87: 02 December 2012 - 03:44 AM

### #8 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: length function for 2D Arrays

Posted 02 December 2012 - 04:09 AM

Two different types of elements? I may have confused you with the name of the array. I should have named them "arrayOfChars," and I apologize if that confused. So, no, the 'type' of the elements are consistent in my example. All of the array elements are characters, and each of the array's ROWS contains an array of characters which happen to be of different lengths. It's mind bending, I know.

In my example (I changed the array name):

char[][] arrayOfChars = new char[3][];

The 3 specifies that there will be 3 arrays that will each contain some number of elements. Alternatively, it might be easier to think of the statement as saying there will be 3 ROWS, each with an unspecified number of COLUMNS containing elements of type char.

As for your code and following comment: Yes, there will be values that will exceed the number of ROWS in that 2D array. I can't see how it defined, but I assume it's consistent with your original question so that 0, 1, 2, and 3 may be the only valid indices. Greater than 3 (or the actual number), and you'll get the error you describe.

Other than that, your code looks setup properly. Some comments would be helpful, and try to avoid the use of "magic numbers," as in the statement "if ( i < 9 )." Either explain why 9 was chosen with a comment or use a CONSTANT (final variable) throughout the program that can be changed easily in one place.

Reputation: 9
• Posts: 197
• Joined: 21-May 12

## Re: length function for 2D Arrays

Posted 03 December 2012 - 01:20 AM

Ok. I get it now. When I put array.length[0] it is checking to see how many columns are in the row for that 0 index. But because my gameboard is a rectangle shape, I can use 0-however many row index's I have because they will all have the same size columns.

It's literally like someone turning on a light bulb in my head lol. I think I was just super tired yesterday.

Thanks so much for the help.

/>

This post has been edited by toad87: 03 December 2012 - 01:21 AM

### #10 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: length function for 2D Arrays

Posted 03 December 2012 - 04:23 AM

Glad to help, and I understand the "light bulb" analogy. I've experienced it often myself during my Java studies, usually during the 5th or 6th tutorial on the same topic. I'm slow but persistent.