为什么 int[] 上的 Arrays.asList 返回 List<int[]>,而不是 List<int>? [英] Why does Arrays.asList on an int[] return a List&lt;int[]&gt;, and not a List&lt;int&gt;?

查看:33
本文介绍了为什么 int[] 上的 Arrays.asList 返回 List<int[]>,而不是 List<int>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个代码:

int[] tcc = {1,2,3};
ArrayList<Integer> tc = Arrays.asList(tcc);

对于上述情况,Java 抱怨它无法从 List 转换为 ArrayList.

For the above, Java complains that it cannot convert from List<int[]> to ArrayList<Integer>.

这是怎么回事?

为什么是 List 而不是 List?

Why is it List<int[]> and not List<int>?

推荐答案

一个 ArrayList 只能保存对象,而不是诸如 int 之类的基元,并且由于 int != Integer 你不能做你想用数组做的事情原语,就这么简单.不过,这将适用于整数数组.

An ArrayList can hold only objects not primitives such as ints, and since int != Integer you can't do what you're trying to do with an array of primitives, simple as that. This will work for an array of Integer though.

这篇关于为什么 int[] 上的 Arrays.asList 返回 List<int[]>,而不是 List<int>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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