File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initializing two dimentional objects array

 
Ali Haider
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,
Can anybody plz tell me that how can i declare a two dimensional objects array......... i mean not a intrinsic type..........
Thankx in advace
Bye
@li
 
Mohamed Yousuff
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have tested the code below and it is working fine.
class test{
public static void main(String ar[]) throws Exception{
Object obj[][] ; // I declare a two dimensional object
obj = new Object[20][] ; // creating a 2d array with 20
// rows
obj[0] = new Object[2] ; // Zero th row -> 2 columns
obj[1] = new Object[3] ; // first row -> 3 columns

// Now I can assign values to the array
obj[0][0] = "string1" ;
obj[0][1] = "String2" ;
obj[1][0] = new java.util.Vector() ;
obj[1][1] = new java.lang.String("Test") ;
obj[1][2] = "String3" ;
// like this you can store anything in this array
}
}
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be precise that is not a 2 dimensional array, it is an array of arrays.
There are some languages that support BOTH.
A multi-dimensional array wuold be an array with more than one parameter in the single array:
String[] myArray = new String[3x4]; //not possible in Java
An array of arrays would be
String[][] myArray = new String [3][4]; //can do in Java
A slight nuance but good to know.
 
Ali Haider
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx to both of u. I need to know that how do i declare the array when i'll use the two dimenional array.
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if your last post is a question, but here goes...
class T {
public static void main (String[] a) {
//declaration
T[][] array;
T[][] array2;
//one way of initializing the array. All values in the array will be set to null.
array = new T[2][2];
//another way of initializing the array. You choose the values and the size of the array yourself.
array2 = new T[][] { { new T(), new T() }, { new T(), new T() } };
//You can refer to any value in the nested arrays by:
T a2= array[0][1];
//or pick out any array in the array by:
T[] a3 = array[0];
}
}

Hope this helps!
/Kaspar
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic