是否有办法将整数列表转换为整数列(不是整数)。像列表int []?没有循环遍历列表并手动将intger转换为int。将整数列表转换为int数组
回答
我确定你可以在第三方库中找到某些东西,但我不相信Java标准库中内置了任何东西。
我建议你只是写一个实用函数来做到这一点,除非你需要很多类似的功能(在这种情况下,它是值得找到相关的第三方库)。请注意,您需要弄清楚如何处理列表中的空引用,这显然无法在int数组中精确表示。
@Downvoter:关心评论? – 2011-02-24 08:01:35
否:)
您需要遍历列表。它不应该太痛苦。
您可以使用toArray
从apache commons中获取Integers
,ArrayUtils
的数组,以将其转换为int[]
。
List<Integer> integerList = new ArrayList<Integer>();
Integer[] integerArray = integerList.toArray(new Integer[0]);
int[] intArray = ArrayUtils.toPrimitive(integerArray);
资源:
- Apache commons -
ArrayUtils.toPrimitive(Integer[])
- Apache commons lang
- Javadoc -
Collection.toArray(T[])
上的同一主题:
+1,即将张贴相同。 :-) – missingfaktor 2010-09-14 06:17:00
有一个错字,它应该是'ArrayUtils'。 – gpeche 2010-09-14 06:39:01
你是对的,谢谢。 – 2010-09-14 06:41:33
下面是整数的集合转换为整数的阵列的实用程序方法。如果输入为空,则返回null。如果输入包含任何空值,则会创建防御副本,并从中删除所有空值。原始集合保持不变。
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;
}
更新:Guava有此功能的一行代码:
int[] array = Ints.toArray(data);
参考:
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())};
我认为这应该可以解决你的问题
- 1. 将列表转换为int数组
- 2. 将int列表转换为numpy数组
- 3. 从int数组转换为整数数组列表中的java
- 4. 如何将int数组转换为数组列表?
- 5. 将列表转换为整数列表
- 6. 将GUID数组转换为int数组
- 7. 将char数组转换为int数组?
- 8. 将数组转换为数组列表
- 9. 将数组列表转换为数组
- 10. 将数组转换为数组列表?
- 11. 将数组列表转换为数组
- 12. 如何将int数组转换为int?
- 13. 转换int数组为int
- 14. 将整数转换为数组数组
- 15. 如何将int数组转换为可为null的int数组?
- 16. Haskell将整数转换为Int?
- 17. 使用jaxb将整数转换为int
- 18. java - 无法将整数转换为int
- 19. 将整数列表转换为列表元组
- 20. 在Java中将整数转换为int数组
- 21. 将整数集转换为int的问题[]数组
- 22. 将整数对象的ArrayList转换为一个int数组?
- 23. 将一个int数组转换为一个整数
- 24. 将整数列表转换为块
- 25. 将整数转换为单词列表
- 26. 将整数转换为列表?
- 27. 将整数数组转换为数字
- 28. 将整数转换为数组数字
- 29. 将逗号分隔的整数列表转换为数组
- 30. 将整数转换为组合框中的数字列表 - C#
循环这里有什么问题? – 2010-09-14 12:12:11