java 将集合转换为 int 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10664698/
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
Conversion of collection to int array
提问by Xara
I want to display a graph and for displaying that i need integer values.I get this from my code
我想显示一个图形并显示我需要整数值。我从我的代码中得到这个
Collection c = Sort.values();
Is there any way that i convert collection in such a way that i get integer values?i get this when i print the collection c
有什么方法可以让我以得到整数值的方式转换集合?我在打印集合 c 时得到这个
[64770, 26529, 13028, 848, 752, 496]
回答by dasblinkenlight
Assuming the values are of type Integer
, you can try this:
假设值是 type Integer
,你可以试试这个:
Collection c = Sort.values();
Integer[] a = (Integer[])(c.toArray(new Integer[c.size()]));
回答by Mattias Isegran Bergander
for (Integer value : c) {
int i = value.intValue();
//do something with either value or i
}
回答by Amr Lotfy
Simply:
简单地:
Integer[] yourArrayVar = yourCollectionVar.toArray(new Integer[0]);
java just needs to know what kind of array to produce.
java只需要知道要生成什么样的数组。
回答by Kaplan
The question was:conversion to intarrayInteger[]
can not be assigned to int[]
or vice versa
问题是:转换为int数组Integer[]
不能赋值,int[]
反之亦然
int[] array = c.stream().mapToInt( i -> i ).toArray();