Java Arrays.asList 在原始数组类型上产生意外的 List 类型 [英] Java Arrays.asList on primitive array type produces unexpected List type

查看:56
本文介绍了Java Arrays.asList 在原始数组类型上产生意外的 List 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
Arrays.asList() 无法正常工作?

显然Arrays.asList(new int[] { 1, 2, 3 });的返回类型是List.这对我来说似乎完全坏了.这与 Java 没有自动装箱原始类型数组有关吗?

Apparently the return type of Arrays.asList(new int[] { 1, 2, 3 }); is List<int[]>. This seems totally broken to me. Does this have something to do with Java not autoboxing arrays of primitive types?

推荐答案

问题在于Arrays.asList 需要一个T...array 的参数.传递 int[] 时唯一适用的 Tint[],因为基元数组不会自动装箱到相应的数组对象类型(在本例中为 Integer[]).

The problem is that Arrays.asList takes a parameter of T... array. The only applicable T when you pass the int[] is int[], as arrays of primitives will not be autoboxed to arrays of the corresponding object type (in this case Integer[]).

所以你可以做 Arrays.asList(new Integer[] {1, 2, 3});.

这篇关于Java Arrays.asList 在原始数组类型上产生意外的 List 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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