aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to initialize a 2 dimension array using brace braket? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to initialize a 2 dimension array using brace braket?" Watch "how to initialize a 2 dimension array using brace braket?" New topic
Author

how to initialize a 2 dimension array using brace braket?

kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
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

Joined: Dec 09, 2000
Posts: 214
Hi,

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

Cheers,

Simon.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Jesse Torres
Ranch Hand

Joined: Mar 25, 2004
Posts: 985
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

Joined: Jun 05, 2001
Posts: 128
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

Joined: Jul 30, 2003
Posts: 804
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...


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
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!!!


SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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


Tony Morris
Java Q&A (FAQ, Trivia)
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to initialize a 2 dimension array using brace braket?