Java的Arrays.asList的基本数组类型产生意想不到的列表类型 [英] Java Arrays.asList on primitive array type produces unexpected List type

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

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1467913/arrays-aslist-not-working-as-it-should\">Arrays.asList()没有工作,因为它应该?

显然 Arrays.asList的返回类型(新INT [] {1,2,3}); 列表&LT; INT [] &GT; 。这似乎完全破碎的我。这是否有些事情要与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的参数.. 。阵列。唯一适用 T 当您通过 INT [] INT [] ,作为原语的阵列将不会autoboxed到相应的对象类型的阵列(在此情况下整数[] )。

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(新的整数[] {1,2,3});

这篇关于Java的Arrays.asList的基本数组类型产生意想不到的列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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