Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

How do you copy multidimension array from Vector to Object[][] ?

Thread view: 
Ronin - 04 Jan 2007 17:00 GMT
I have absolutely no idea how to do this. I inserted a [] array into a
vector, which then ofcourse became a [][] array, but now can't get it
out back to a Object[][].

Vector vector = new Vector();
vector.addElement(new Object[]{"A","B"});
vector.addElement(new Object[]{"C","D"});

Object obj[] = new Object[m.size()];        //this works so far

m.copyInto((Object[]) obj);        //this works so far
Object[][] attribute = (Object[][]) obj;  //BANG!!!

why the hell wont it work?

for now I'll just use the crappy m.elementAt() function and cast to [].
All this casting in a loop eats CPU though.
Chris - 04 Jan 2007 22:40 GMT
> I have absolutely no idea how to do this. I inserted a [] array into a
> vector, which then ofcourse became a [][] array, but now can't get it
[quoted text clipped - 13 lines]
> for now I'll just use the crappy m.elementAt() function and cast to [].
> All this casting in a loop eats CPU though.

Some random, maybe not-useful ideas:

-- Don't use Vector. Use ArrayList.

-- ArrayList.toArray() is helpful

-- One of the problems with the Collection classes is that you have to
cast everything. Generics don't help, because it still does a cast
internally. If you have a performance-intensive app, don't use
Collections. Write your own wrapper around an array of the proper type.
(But *only* if know for sure that casts are causing a performance problem).
Ronin - 05 Jan 2007 06:40 GMT
Hi Chris thanks but I cant use ArrayList as I'm using J2ME. The API
does not have that class.

the thing is im reading data from a server and I dont know the size
beforehand. So I'm first dropping it into a Vector than move it to a
Object[][] so to save the whole casting shebang when im iterating
through it.

If there is no solution to port to Object[][] from vector.copyInto,
then yes I'll have to write my own little Vector class. I'll keep my
eye on this thread for a while.

cheers

> > I have absolutely no idea how to do this. I inserted a [] array into a
> > vector, which then ofcourse became a [][] array, but now can't get it
[quoted text clipped - 25 lines]
> Collections. Write your own wrapper around an array of the proper type.
> (But *only* if know for sure that casts are causing a performance problem).
Ronin - 05 Jan 2007 06:51 GMT
Christ! what a good night of sleep can do!

Here the solution:

Vector vector = new Vector();
       vector.addElement(new Object[]{"A","B"});
       vector.addElement(new Object[]{"C","D"});
       Object obj[][] = new Object[vector.size()][2];
       vector.copyInto((Object[]) obj);

> Hi Chris thanks but I cant use ArrayList as I'm using J2ME. The API
> does not have that class.
[quoted text clipped - 39 lines]
> > Collections. Write your own wrapper around an array of the proper type.
> > (But *only* if know for sure that casts are causing a performance problem).
Ronin - 05 Jan 2007 08:20 GMT
Just for those that want to use Vector.toArray() methods .... no such
thing in J2ME

its hacking all the way.

> Christ! what a good night of sleep can do!
>
[quoted text clipped - 49 lines]
> > > Collections. Write your own wrapper around an array of the proper type.
> > > (But *only* if know for sure that casts are causing a performance problem).


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.