java 将整数集转换为 int[] 数组的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7123580/
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 18:43:40  来源:igfitidea点击:

Problems converting Set of Integers to int[] array

javacollections

提问by L-Samuels

I have no problems converting a Set of Strings to a string[] array, but I'm having problems doing so with converting a Set of Integer to an int[] array. How can I convert the Integers to its primitive?

我在将一组字符串转换为 string[] 数组时没有问题,但是在将一组整数转换为 int[] 数组时遇到了问题。如何将整数转换为其原始类型?

I cannot seem to find any related questions. Any quick suggestions that can help?

我似乎找不到任何相关的问题。任何可以提供帮助的快速建议?

Sometimes, autoboxing cannot be used, as in the case of arrays. I don't think an array of integers will automatically be converted to an array of ints.

有时,不能使用自动装箱,例如数组。我认为整数数组不会自动转换为整数数组。

回答by Thomas

string[]doesn't exist, I guess you mean String[].

string[]不存在,我猜你的意思是String[]

For converting a Set<Integer>to int[]you'd have to iterate over the set manually.

Set<Integer>要将a 转换为int[]您必须手动迭代该集合。

Like this:

像这样:

Set<Integer> set = ...;

int[] arr = new int[set.size()];

int index = 0;

for( Integer i : set ) {
  arr[index++] = i; //note the autounboxing here
}

Note that sets don't have any particular order, if the order is important, you'd need to use a SortedSet.

请注意,集合没有任何特定顺序,如果顺序很重要,则需要使用 SortedSet.

回答by Kevin Bourrillion

This is why Guava has an Ints.toArray(Collection<Integer>)method, returning int[].

这就是为什么 Guava 有一个Ints.toArray(Collection<Integer>)方法,返回int[].

回答by linh nguyenvan

With java 8:

使用 Java 8:

Set<Integer> set = new HashSet<>(); 
// TODO: Add implement for set

int[] array = set.stream().mapToInt(Integer::intValue).toArray();

回答by Péter T?r?k

I guess the problem is that Set<Integer>.toArrayconverts to Integer[], rather than int[]. So you have no simple way: you need to iterate through the set manually and add its elements to the int array. Converting an individual Integerto intis handled by autoboxing in Java 5 and above.

我想问题是Set<Integer>.toArray转换为Integer[], 而不是int[]. 所以你没有简单的方法:你需要手动遍历集合并将其元素添加到 int 数组中。转换的个体Integer,以int通过在Java 5及更高自动装箱处理。

回答by filip-fku

This should work, assuming auto unboxing!

这应该有效,假设自动拆箱!

Set<Integer> myIntegers; // your set
int[] ints = new int[myIntegers.size()];
int index = 0;
for(Integer i : myIntegers){
    ints[index++] = i;
}

回答by MozenRath

you can call the

你可以打电话给

Integer.intValue();

function...

功能...

lemme know more specifics of what you need :)

让我知道你需要什么的更多细节:)

回答by Cygnusx1

If you use Java 5+ Autoboxing should take care of this...!

如果您使用 Java 5+ 自动装箱应该注意这一点...!

What error do you get?

你得到什么错误?

edit: ok i see..

编辑:好的,我明白了..

Like other said:

就像其他人说的:

loop on your Set and just put the Integer inside the int[], autoboxing should convert it.

在你的 Set 上循环并将整数放在 int[] 中,自动装箱应该转换它。