File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes initializing two dimentional objects array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "initializing two dimentional objects array" Watch "initializing two dimentional objects array" New topic
Author

initializing two dimentional objects array

Ali Haider
Greenhorn

Joined: Feb 21, 2001
Posts: 25
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

Joined: Jun 23, 2001
Posts: 73

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

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Ali Haider
Greenhorn

Joined: Feb 21, 2001
Posts: 25
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

Joined: Jun 18, 2001
Posts: 128
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
 
subject: initializing two dimentional objects array