开发者

how to convert Vector<Integer> to an int[]? [duplicate]

This question already has answers here: Closed 11 years ago.

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜