• 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

how to initialize a 2 dimension array using brace braket?

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Cheers,

Simon.
 
Ranch Hand
Posts: 985
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic