Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector.toArray(object [] a) ?

 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I have a Vector that I previously filled with String values coming from a String[][] array.
Now , I need to get back my String [][] array from the vector.
The method Vector.toArray(Object[] a) doesn't works and I don't knwo why.

This code will not work and will throw a java.lang.ArrayStoreException,
unless I uncomment the for loop and comment this line :columns=(String[])subVector.toArray(columns);
I don't understand why? Isn't it supposed to do the same?
If anybody has an idea, I would be glad to understand.
Thx in advance
[ April 03, 2003: Message edited by: Younes Essouabni ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Younes,
You must be passing in a subvector that has some element other a String somewhere. I tried it both ways with this:

And got this output both times:

Michael Morris
 
Weerawit Maneepongsawat
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

may be like this..i holp this help.
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Michael, thank you Weerawit.
It sees that you were right, there isn't only string object.
That's the reason why this one works: columns[j]=subVector.elementAt(j).toString();
and this one doesn't works : columns[j]=(String) subVector.elementAt(j);
I thought that both were the same, but it is not. One is "translating" an object into a string, and the other is casting an object into a string, which is permitted as long as the object effectively contains a string.
Thank you for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic