• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to convert Vector of Vectors to String[] []  RSS feed

 
tim parks
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
a java prob for you. I have a vector of vectors. The inner vector has values that are Strings. I want to convert the Vector of vectors into a String[][] 2-d array. Thought I might be able to do it in a single line
stuff = (String[][])data.toArray();
but had problems. So tried to iterate over each inner vector. But I'm getting a classcast exception with the line onfiltered= (String[])content.toArray();

Any ideas?
thanks
T.

public class tester{
public static void main(String[] args) {
Vector rowValues = new Vector();
Vector data = new Vector();
//String[][] stuff;
String[] onfiltered= new String[0];

try{
rowValues.addElement("one");
rowValues.addElement("two");
data.addElement(rowValues);
rowValues = new Vector();
rowValues.addElement("three");
rowValues.addElement("four");
data.addElement(rowValues);
System.out.println(data);
//stuff = (String[][])data.toArray();
Iterator row = data.iterator();
while(row.hasNext()) {
Vector content = (Vector) row.next();
onfiltered= (String[])content.toArray();
}
}catch(Exception e){
System.out.println(e);
}
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Do try to format your codes next time, its helps you by helping people to read your codes. Also, you sounded a bit rude, seems like you're demanding a solution in here, things don't work this way...

Anyway, when you invoked toArray(), an Object[] is returned. Basically a Vector or Object[] can contain different element types. There's no guarantee that the entire Vector or Object[] holds the same class object, hence, you can't type cast it to String[].

So, go a bit deeper by iterating through the individual elements.

Hope this helps.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Format code between [C0DE] [/C0DE] markers ( replace the zeros with ohs)
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

Here's the code reformatted, to make it easier to read:



I've also changed one line, to remove the ClassCastException. I've never investigated why this method works while yours doesn't, but it's a start. I have a suspicion it might be in the implementation of toArray(), but that's utterly unfounded.

On a side note, by convention classes are given an initial capital letter: 'Tester' not 'tester'.
--Tim
[ June 27, 2004: Message edited by: Tim West ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a suspicion it might be in the implementation of toArray(), but that's utterly unfounded.

Yes, it's a difference between toArray() and toArray(Object[]). The former always returns a plain Object[] array, not a subtype, regardless of what class the contents of the array may have. The latter returns an array whose type matches the type of the parameter. So toArray(new String[x]) returns a String[], while toArray(new Integer[x]) returns an Integer[].
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim -

Oh cool - Makes sense now. Thanks for that!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!