Arrays.asList() 不能正常工作? [英] Arrays.asList() not working as it should?

查看:29
本文介绍了Arrays.asList() 不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 float[],我想得到一个具有相同元素的列表.我可以做一件一件一件很丑的事情,但我想使用 Arrays.asList 方法.但是有一个问题.这有效:

I have a float[] and i would like to get a list with the same elements. I could do the ugly thing of adding them one by one but i wanted to use the Arrays.asList method. There is a problem though. This works:

List<Integer> list = Arrays.asList(1,2,3,4,5);

但这不是.

int[] ints = new int[] {1,2,3,4,5};
List<Integer> list = Arrays.asList(ints);

asList 方法接受一个 varargs 参数,据我所知,它是数组的简写".

The asList method accepts a varargs parameter which to the extends of my knowledge is a "shorthand" for an array.

问题:

  • 为什么第二段代码返回的是 List 而不是 List.

有办法改正吗?

为什么自动装箱在这里不起作用;即 int[]Integer[]?

Why doesn't autoboxing work here; i.e. int[] to Integer[]?

推荐答案

这个怎么样?

Integer[] ints = new Integer[] {1,2,3,4,5};
List<Integer> list = Arrays.asList(ints);

这篇关于Arrays.asList() 不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆