Java 将整数列表转换为整数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3706470/
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
Convert Integer List to int array
提问by Kamal
Is there a way, to convert a List of Integers to Array of ints (not integer). Something like List to int []? Without looping through the list and manually converting the intger to int.
有没有办法将整数列表转换为整数数组(不是整数)。像 List 到 int [] 之类的东西?无需遍历列表并手动将整数转换为整数。
采纳答案by Jon Skeet
I'm sure you can find something in a third-party library, but I don't believe there's anything built into the Java standard libraries.
我相信您可以在第三方库中找到一些东西,但我不相信 Java 标准库中内置了任何东西。
I suggest you just write a utility function to do it, unless you need lots of similar functionality (in which case it would be worth finding the relevant 3rd party library). Note that you'll need to work out what to do with a null reference in the list, which clearly can't be represented accurately in the int array.
我建议你只写一个实用函数来完成它,除非你需要很多类似的功能(在这种情况下,找到相关的 3rd 方库是值得的)。请注意,您需要弄清楚如何处理列表中的空引用,这显然无法在 int 数组中准确表示。
回答by davmac
No :)
不 :)
You need to iterate through the list. It shouldn't be too painful.
您需要遍历列表。应该不会太痛。
回答by Colin Hebert
You can use the toArray
to get an array of Integers
, ArrayUtils
from the apache commons to convert it to an int[]
.
您可以使用toArray
获得的阵列Integers
,ArrayUtils
从Apache公地将其转换为一个int[]
。
List<Integer> integerList = new ArrayList<Integer>();
Integer[] integerArray = integerList.toArray(new Integer[0]);
int[] intArray = ArrayUtils.toPrimitive(integerArray);
Resources :
资源 :
- Apache commons -
ArrayUtils.toPrimitive(Integer[])
- Apache commons lang
- Javadoc -
Collection.toArray(T[])
On the same topic :
在同一主题上:
回答by Sean Patrick Floyd
Here is a utility method that converts a Collection of Integers to an array of ints. If the input is null, null is returned. If the input contains any null values, a defensive copy is created, stripping all null values from it. The original collection is left unchanged.
这是一个将整数集合转换为整数数组的实用方法。如果输入为空,则返回空。如果输入包含任何空值,则会创建一个防御性副本,从中剥离所有空值。原始集合保持不变。
public static int[] toIntArray(final Collection<Integer> data){
int[] result;
// null result for null input
if(data == null){
result = null;
// empty array for empty collection
} else if(data.isEmpty()){
result = new int[0];
} else{
final Collection<Integer> effective;
// if data contains null make defensive copy
// and remove null values
if(data.contains(null)){
effective = new ArrayList<Integer>(data);
while(effective.remove(null)){}
// otherwise use original collection
}else{
effective = data;
}
result = new int[effective.size()];
int offset = 0;
// store values
for(final Integer i : effective){
result[offset++] = i.intValue();
}
}
return result;
}
Update:Guavahas a one-liner for this functionality:
更新:Guava有一个用于此功能的单行:
int[] array = Ints.toArray(data);
Reference:
参考:
回答by CodeMadness
List<Integer> listInt = new ArrayList<Integer>();
StringBuffer strBuffer = new StringBuffer();
for(Object o:listInt){
strBuffer.append(o);
}
int [] arrayInt = new int[]{Integer.parseInt(strBuffer.toString())};
I think this should solve your problem
我认为这应该可以解决您的问题