[英]Conversion of collection to int array
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]
Assuming the values are of type Integer
, you can try this:假设这些值是Integer
类型,你可以试试这个:
Collection c = Sort.values();
Integer[] a = (Integer[])(c.toArray(new Integer[c.size()]));
for (Integer value : c) {
int i = value.intValue();
//do something with either value or i
}
The question was: conversion to int array问题是:转换为int数组Integer[]
can not be assigned to int[]
or vice versa Integer[]
不能赋值给int[]
,反之亦然
int[] array = c.stream().mapToInt( i -> i ).toArray();
Simply:简单地:
Integer[] yourArrayVar = yourCollectionVar.toArray(new Integer[0]);
java just needs to know what kind of array to produce. java只需要知道要生成什么样的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.