Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to store data in multi dimenstional arrays

 
Karan Raj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Karan Raj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jhonson, your solution is simple and easy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic