how to convert Vector<Integer> to an int[]? [duplicate]
Possible Duplicate:
How to convert List<Integer> to int[] in Java?
Is there some method to convert a Vector< Integer> to an int[]?
Thank开发者_运维百科s
toArray()
will do the conversion for you. Check the link for the javadoc of all the methods Vector
has. This will be the boxed Integer
, not int
, but you can work from there.
Integer[] sl = (Integer[]) myVector.toArray(new Integer[0]);
Vector uses objects and not primary types. so you can only convert to an Object[], to convert to a primary type array you'd have to use an additional step.
with no further comments on what's the point of your code, I would say that Integer[] would acomplish the same thing
You can use a loop to copy a vector into an int[]
Vector<Integer> vector = ....
int count = 0, ints[] = new int[vector.size()];
for(int i: vector) ints[count++] = i;
精彩评论