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

how to initialize a 2 dimension array using brace braket?

 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
suppose i have the following



i know the above is not legal since list is a 2D array...but how should I initialize it using {}?

thanks..
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

int [] [] x = {{0,1}, {2,3}};

Cheers,

Simon.
 
Jesse Torres
Ranch Hand
Posts: 985
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int [][] list = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

for(int row = 0; row < list.length; row++) {

for(int column = 0; column < list[row].length; column++) {
System.out.print(list[row][column] + " ");
}
System.out.println();
}
 
Sonny Pondrom
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have specified 7 items in a 2D array. That could be a [2]x[4] with a zero or a [1]x[7] with seven zeros. Or many others if zeros are assumed.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not require that subarrays must have the same number of elements. You could easily have a two-d array [5][], which is an array of five arrays. Each contained array could have a different number of elements...
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, the following are way to declare 2D array in java.

1. int [][] array = new int [2][];
2. int [] array[] = new int [4][];
3. int []array [] = {{2,4,5,6},{2,3,4,5}};

Whao ! hope it helps. All the best of luck!!!
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java does not require that subarrays must have the same number of elements.


Specifically, Java does not have multi-dimensional arrays.
http://www.xdweb.net/~dibblego/java/faq/answers.html#q45
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems more a point of semantics to me.

From The Java Tutorial (by Sun), under the topic Creating Arrays:
You can also use the newInstance method to create multidimensional arrays. In this case, the parameters of the method are the component type and an array of int types representing the dimensions of the new array.

The next sample program shows how to use newInstance to create multidimensional arrays:


From the topic Retrieving Component Types:
The component type of a multidimensional array is an array. In the next line of code, the component type of the array named matrix is int[]:
int[][] matrix = new int[100][100];

[ February 16, 2005: Message edited by: Jeff Bosch ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic