Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conver to String[][]

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of:
list.toArray()
use:
(String [][]) list.toArray(new String[0][])
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot!!!
 
AJ2
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will highly suggest using Vector instead of ArrayList since Vectors are synchronized and Arraylist are not.
 
David Li
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Thomas Fly
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you posted a few lines of the problematic code, like:
 
David Li
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tybon Wu
Thanks. I make a low-level mistake. But I get the point now.
Thanks for your help.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Fly:
[QB]It would help if you posted a few lines of the problematic code... [QB]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your method signature saying it returns a String[][]?
Mark
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic