如何在 Java 中将 int[] 转换为 List<Integer>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1073919/
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
How to convert int[] into List<Integer> in Java?
提问by pupeno
How do I convert int[]
into List<Integer>
in Java?
我如何转换int[]
成List<Integer>
Java?
Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not part of Java.
当然,我对任何其他答案都感兴趣,而不是逐项循环进行。但是,如果没有其他答案,我会选择那个最佳答案,以表明此功能不是 Java 的一部分。
采纳答案by willcodejavaforfood
There is no shortcut for converting from int[]
to List<Integer>
as Arrays.asList
does not deal with boxing and will just create a List<int[]>
which is not what you want. You have to make a utility method.
没有从int[]
to转换的捷径,List<Integer>
因为Arrays.asList
它不处理拳击,只会创建一个List<int[]>
不是你想要的。你必须制定一个实用方法。
int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints)
{
intList.add(i);
}
回答by Adamski
It's also worth checking out this bug report, which was closed with reason "Not a defect" and the following text:
还值得查看此错误报告,该报告因“不是缺陷”和以下文本而关闭:
"Autoboxing of entire arrays is not specified behavior, for good reason. It can be prohibitively expensive for large arrays."
“整个数组的自动装箱不是指定的行为,这是有充分理由的。对于大型数组来说,它可能会非常昂贵。”
回答by Leonel
Arrays.asList will not work as some of the other answers expect.
Arrays.asList 不会像其他一些答案所期望的那样工作。
This code will notcreate a list of 10 integers. It will print 1, not 10:
此代码不会创建 10 个整数的列表。它将打印1,而不是10:
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List lst = Arrays.asList(arr);
System.out.println(lst.size());
This will create a list of integers:
这将创建一个整数列表:
List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
If you already have the array of ints, there is not quick way to convert, you're better off with the loop.
如果您已经拥有整数数组,则没有快速转换的方法,最好使用循环。
On the other hand, if your array has Objects, not primitives in it, Arrays.asList will work:
另一方面,如果您的数组中有对象,而不是其中的基元,则 Arrays.asList 将起作用:
String str[] = { "Homer", "Marge", "Bart", "Lisa", "Maggie" };
List<String> lst = Arrays.asList(str);
回答by dfa
give a try to this class:
试试这个类:
class PrimitiveWrapper<T> extends AbstractList<T> {
private final T[] data;
private PrimitiveWrapper(T[] data) {
this.data = data; // you can clone this array for preventing aliasing
}
public static <T> List<T> ofIntegers(int... data) {
return new PrimitiveWrapper(toBoxedArray(Integer.class, data));
}
public static <T> List<T> ofCharacters(char... data) {
return new PrimitiveWrapper(toBoxedArray(Character.class, data));
}
public static <T> List<T> ofDoubles(double... data) {
return new PrimitiveWrapper(toBoxedArray(Double.class, data));
}
// ditto for byte, float, boolean, long
private static <T> T[] toBoxedArray(Class<T> boxClass, Object components) {
final int length = Array.getLength(components);
Object res = Array.newInstance(boxClass, length);
for (int i = 0; i < length; i++) {
Array.set(res, i, Array.get(components, i));
}
return (T[]) res;
}
@Override
public T get(int index) {
return data[index];
}
@Override
public int size() {
return data.length;
}
}
testcase:
测试用例:
List<Integer> ints = PrimitiveWrapper.ofIntegers(10, 20);
List<Double> doubles = PrimitiveWrapper.ofDoubles(10, 20);
// etc
回答by Christoffer
I'll add another answer with a different method; no loop but an anonymous class that will utilize the autoboxing features:
我会用不同的方法添加另一个答案;没有循环,而是一个匿名类,它将利用自动装箱功能:
public List<Integer> asList(final int[] is)
{
return new AbstractList<Integer>() {
public Integer get(int i) { return is[i]; }
public int size() { return is.length; }
};
}
回答by Kannan Ekanath
The smallest piece of code would be:
最小的一段代码是:
public List<Integer> myWork(int[] array) {
return Arrays.asList(ArrayUtils.toObject(array));
}
where ArrayUtils comes from commons-lang :)
其中 ArrayUtils 来自 commons-lang :)
回答by louisgab
Also from guava libraries... com.google.common.primitives.Ints:
同样来自番石榴图书馆...... com.google.common.primitives.Ints:
List<Integer> Ints.asList(int...)
回答by Daniel De León
The best shot:
最佳拍摄:
**
* Integer modifiable fix length list of an int array or many int's.
*
* @author Daniel De Leon.
*/
public class IntegerListWrap extends AbstractList<Integer> {
int[] data;
public IntegerListWrap(int... data) {
this.data = data;
}
@Override
public Integer get(int index) {
return data[index];
}
@Override
public Integer set(int index, Integer element) {
int r = data[index];
data[index] = element;
return r;
}
@Override
public int size() {
return data.length;
}
}
- Support get and set.
- No memory data duplication.
- No wasting time in loops.
- 支持获取和设置。
- 无内存数据重复。
- 不要在循环中浪费时间。
Examples:
例子:
int[] intArray = new int[]{1, 2, 3};
List<Integer> integerListWrap = new IntegerListWrap(intArray);
List<Integer> integerListWrap1 = new IntegerListWrap(1, 2, 3);
回答by user2037659
In Java 8 with stream:
在带有流的 Java 8 中:
int[] ints = {1, 2, 3};
List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, Arrays.stream(ints).boxed().toArray(Integer[]::new));
or with Collectors
或与收藏家
List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList());
回答by mikeyreilly
Streams
流
In Java 8 you can do this
在 Java 8 中你可以这样做
int[] ints = {1,2,3};
List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList());