Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multidimensional Arrays

 
Mark Grizzaffi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Mark Grizzaffi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21407
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Israel Guerra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Henry. That clears it up.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic