• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multidimensional Arrays

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);

}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry. That clears it up.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Everybody's invited. Even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic