aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes conver to String[][] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "conver to String[][]" Watch "conver to String[][]" New topic
Author

conver to String[][]

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi All,
I'm using the ArrayList to store the data from database.
I use list.toArray() it return Object[],
I'm wonding there is any method form jdk that can conver object[] into String[][].
As in my tablemodel access String[][] for the table data.
thanks,
lili
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
Instead of:
list.toArray()
use:
(String [][]) list.toArray(new String[0][])


SCJP2
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
thanks a lot!!!
AJ2
Greenhorn

Joined: Oct 04, 2002
Posts: 4
I will highly suggest using Vector instead of ArrayList since Vectors are synchronized and Arraylist are not.
David Li
Ranch Hand

Joined: Aug 12, 2002
Posts: 41
Tybon Wu
When I call the code :
(String [][]) list.toArray(new String[0][])
I got the exception:
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(ArrayList.java:301)
at Test.main(Test.java:15)
Where is my mistake?


David,<br />SCJP - 94% (2002-7-6)
Thomas Fly
Ranch Hand

Joined: Sep 09, 2002
Posts: 164
It would help if you posted a few lines of the problematic code, like:


Fly by Night Consultants<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr><i>I climbed on the back of a giant albatross<br />which flew through a crack in the cloud<br />to a place where happiness reigned...<br />all year 'round<br />the music played ever so loudly!</i><p><a href="http://thomasfly.com/songs/Traffic/Hole_in_my_Shoe_qt.htm" target="_blank" rel="nofollow">Hole in My Shoe</a><hr></blockquote>
David Li
Ranch Hand

Joined: Aug 12, 2002
Posts: 41
Thomas Fly
Thanks for your reply.
Of course your code is correct. But if I add some data into the ArrayList, then it will get a exception.
Please check the following code:
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84

You are trying to convert String[] into String[][] in your code. Every element of your ArrayList must be of type String[] in order to produce an object of type String[][] when you call the toArray method. The element "1" is not of type String[], therefore the runtime exceptions occurs.
David Li
Ranch Hand

Joined: Aug 12, 2002
Posts: 41
Tybon Wu
Thanks. I make a low-level mistake. But I get the point now.
Thanks for your help.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"AJ2"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks! and welcome to the JavaRanch!


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Actually the ArrayList is preferred over the Vector.
Vectors and HashTables are considered the older Collections way, and the newer collections are faster and preferred.
Mark
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi all,
I am working on RMI, It looks OK but i've
a problem with this line again.
(String [][]) list.toArray(new String[0][])
it can't pass the String[][] as result back to client. It was working in local mode.
any suggestion please, Thanks..
lili
Thomas Fly
Ranch Hand

Joined: Sep 09, 2002
Posts: 164
Originally posted by Thomas Fly:
[QB]It would help if you posted a few lines of the problematic code... [QB]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Is your method signature saying it returns a String[][]?
Mark
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for all the reply,
I have solved the problem by moving this part back to client site.
I just feeling that we need to often change design structure while we are in development process.
Cheers,
lili
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conver to String[][]
 
Similar Threads
Using JavaBean to keep record info
ASCII to UNICODE
How to pass XML Document from one EJB to another EJB?
another assert question
Rounding up double value