jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Multidimensional Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multidimensional Arrays" Watch "Multidimensional Arrays" New topic
Author

Multidimensional Arrays

Mark Grizzaffi
Greenhorn

Joined: Jul 16, 2007
Posts: 6
What is the difference between:
int[][] myArray = new [5][];
and
int[][] myArray = new [5][1];

I understand the first initialization as two arrays each containing 5 int arrays.
What is the second initialization indicating?

Thanks
Israel Guerra
Greenhorn

Joined: Jul 12, 2007
Posts: 3
Greetings to all, this is my first post in the forum!

Mark, i checked it and i think it's like this:

int[][] myArray = new [5][];

In this case, like you mentioned, each position of myArray can reference an array object.

int[][] myArray = new [5][1];

This second situation means that you are declaring an array with 5 positions, each referencing an array with one postion.

Hope you understood.

Best regards,

--
Guerra


--<br />Guerra
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome, both of you, to JavaRanch!

I think Israel already said this, but I think I could state it more clearlt. The one with the empty braces is declaring a five-element array, with each element set to null. The type of each element is int[], meaning that you could set each element to point to an array of int, but each element is empty. You'd still need to create those other arrays. As it is, this 2-D array can hold exactly zero ints.

In the second case, each of those five elements contains an actual int array of length 1. This 2-D array can hold 5 ints, one in each one-element array.


[Jess in Action][AskingGoodQuestions]
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by Israel Guerra:
Greetings to all, this is my first post in the forum!

Mark, i checked it and i think it's like this:

int[][] myArray = new [5][];

In this case, like you mentioned, each position of myArray can reference an array object.

int[][] myArray = new [5][1];

This second situation means that you are declaring an array with 5 positions, each referencing an array with one postion.

Hope you understood.

Best regards,

--
Guerra


Correct answer but, allow me to correct the code:



Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Mark Grizzaffi
Greenhorn

Joined: Jul 16, 2007
Posts: 6
Thanks, guys. But based on the explanation:

"This second situation means that you are declaring an array with 5 positions, each referencing an array with one position."

shouldn't I get an exception on something like
myArray[0][1] = 2;
myArray[0][2] = 3;
myArray[0][3] = 3;

where I specify more than one position. (I may not be seeing this correctly)

Thanks



public class TestMultiDimArrays
{
public static void main(String[] args)
{
int [][] myArray = new int[5][1];
myArray[0] = new int[4];
myArray[1] = new int[3];
myArray[2] = new int[5];
myArray[3] = new int[6];
myArray[4] = new int[7];

myArray[0][1] = 2;
myArray[0][2] = 3;
myArray[0][3] = 3;
myArray[1][0] = 6;
myArray[1][1] = 7;
myArray[1][2] = 8;
myArray[2][0] = 9;

System.out.println("myArray[0][0] = " + myArray[0][0]);
System.out.println("myArray[0][1] = " + myArray[0][1]);
System.out.println("myArray[0][2] = " + myArray[0][2]);
System.out.println("myArray[0][3] = " + myArray[0][3]);
System.out.println("myArray[1][0] = " + myArray[1][0]);
System.out.println("myArray[1][1] = " + myArray[1][1]);
System.out.println("myArray[1][2] = " + myArray[1][2]);
System.out.println("myArray[2][0] = " + myArray[2][0]);
System.out.println("myArray[2][1] = " + myArray[2][1]);
System.out.println("myArray[3][0] = " + myArray[3][0]);
System.out.println("myArray[3][1] = " + myArray[3][1]);

}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40



You are declaring an array of size five, each of which is holding an array that holds one int... then... you are replacing each of the five int arrays. The first with an array of size 4, the next of size 3, the next of size 5, the next of size 6, and the last with size 7. The 5 original int arrays have now been dereferenced and eligible for garbage collection.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Israel Guerra
Greenhorn

Joined: Jul 12, 2007
Posts: 3
Correct answer but, allow me to correct the code:

code:


int[][] myArray = new int[5][];
int[][] myArray = new int[5][1];


oops, i just copied it and didn't see it was wrong. Thanks for correcting me Manuel.
Mark Grizzaffi
Greenhorn

Joined: Jul 16, 2007
Posts: 6
Thanks, Henry. That clears it up.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mark Grizzaffi:
...System.out.println("myArray[0][0] = " + myArray[0][0]);
System.out.println("myArray[0][1] = " + myArray[0][1]);
System.out.println("myArray[0][2] = " + myArray[0][2]);
System.out.println("myArray[0][3] = " + myArray[0][3]);
System.out.println("myArray[1][0] = " + myArray[1][0]);
System.out.println("myArray[1][1] = " + myArray[1][1]);
System.out.println("myArray[1][2] = " + myArray[1][2]);
System.out.println("myArray[2][0] = " + myArray[2][0]);
System.out.println("myArray[2][1] = " + myArray[2][1]);
System.out.println("myArray[3][0] = " + myArray[3][0]);
System.out.println("myArray[3][1] = " + myArray[3][1]);...

As a good practice exercise, can you write this as a nested loop with a single println statement?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multidimensional Arrays