Two Laptop Bag*
The moose likes Beginning Java and the fly likes how to store data in multi dimenstional arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to store data in multi dimenstional arrays" Watch "how to store data in multi dimenstional arrays" New topic
Author

how to store data in multi dimenstional arrays

Karan Raj
Greenhorn

Joined: Jan 27, 2004
Posts: 16
Hi,

can anybody tell me ..how to store and retrieve the data from multidimensional arrays ..is there any class or method i can use ..?
Thanks
karan
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
Yes. A multi-dimensional array is simply an array of arrays. Check out this code...it should help you figure out multi-dimensional arrays really quick if you run it!

As you can see, your arrays can be of varying lengths. Stare at the output of this program for awhile, then play around with it. It'll become clear after awhile. Hope this helps!


David Crossett
-nothing important to say, but learnin' plenty-
Karan Raj
Greenhorn

Joined: Jan 27, 2004
Posts: 16
Hi David,

thanks for your inputs.My actual problem is....I don't know the length of the data i want to store. For my purpose i used Vectors for storing.This is the sample code i used for it.
Vector v=new Vector(2,3);
Object temp[]={"ADD332343",new Integer(230),new Double(3.22),
new Integer(22)}

v.addElement(temp);

Object t;
t=v.elementAt(0);

but how i can i refer to the individual elements in the "t"...? .
please help me...i am just learning java
Thanks
Karan
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Once you pull something out of a Vector, you have to cast it to the proper type before you can use it:

You could also combine the get and cast into one line:
Object[] tArr = (Object[])v.elementAt(0);
Obviously if the first element of "v" was something other than an Object array you would get a class cast exception.
Karan Raj
Greenhorn

Joined: Jan 27, 2004
Posts: 16
Thanks jhonson, your solution is simple and easy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to store data in multi dimenstional arrays