java 如何将 Vector<Integer> 转换为 int[]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5703214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert Vector<Integer> to an int[]?
提问by Alex Moreno
Possible Duplicate:
How to convert List<Integer> to int[] in Java?
Is there some method to convert a Vector< Integer> to an int[]?
有什么方法可以将 Vector<Integer> 转换为 int[]?
Thanks
谢谢
采纳答案by unholysampler
回答by MarkPowell
Integer[] sl = (Integer[]) myVector.toArray(new Integer[0]);
回答by IvoC
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.
Vector 使用对象而不是主要类型。所以您只能转换为 Object[],要转换为主要类型数组,您必须使用额外的步骤。
with no further comments on what's the point of your code, I would say that Integer[] would acomplish the same thing
没有进一步评论你的代码有什么意义,我会说 Integer[] 会完成同样的事情
回答by Peter Lawrey
You can use a loop to copy a vector into an int[]
您可以使用循环将向量复制到 int[]
Vector<Integer> vector = ....
int count = 0, ints[] = new int[vector.size()];
for(int i: vector) ints[count++] = i;