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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:23:30  来源:igfitidea点击:

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

javavectorint

提问by Alex Moreno

Possible Duplicate:
How to convert List<Integer> to int[] in Java?

可能的重复:
如何在 Java 中将 List<Integer> 转换为 int[]?

Is there some method to convert a Vector< Integer> to an int[]?

有什么方法可以将 Vector<Integer> 转换为 int[]?

Thanks

谢谢

采纳答案by unholysampler

toArray()will do the conversion for you. Check the link for the javadoc of all the methods Vectorhas. This will be the boxed Integer, not int, but you can work from there.

toArray()将为您进行转换。检查所有方法的 javadoc 链接Vector。这将是盒装的Integer,而不是int,但您可以从那里开始工作。

回答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;