使用Arrays.asList与int数组 [英] Using Arrays.asList with int array

查看:350
本文介绍了使用Arrays.asList与int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 java.util.Arrays.asList ,为什么它显示不同的列表大小 INT (原始类型)和字符串数组?

Using java.util.Arrays.asList, why its shows different list size for int (Primitive type) and String array?

a)对于 INT 阵列,每当我执行下面的程序列表的大小= 1

a) With int array, whenever I execute following program the List size = 1

public static void main(String[] args) {
        int ar[]=new int[]{1,2,3,4,5};
        List list=Arrays.asList(ar);
        System.out.println(list.size());

    }

二)但是,如果我从 INT 数组类型更改为字符串阵列(如字符串AR [] =新的String [] {ABC,荷航,某某,PQR}; ),然后我得到的列表的大小为4,我认为是正确的

b) But if i change from int array type to String array(like String ar[] = new String[]{"abc","klm","xyz","pqr"};) , then I am getting the list size as 4 which i think is correct.

PS :随着整数(包装类)阵列,那么结果是很好,但我为什么在原始的<$ C $我不知道C> INT 数组,列表的大小是1,请解释一下。

PS : With Integer (Wrapper Class) array, then result is Fine, but i am not sure why in primitive int array, the list size is 1. Please explain.

推荐答案

列表不能举行,因为Java泛型的原始值(见的similar问题)。所以,当你调用 Arrays.asList(AR)的阵列与一个项目创建一个列表 - 的int数组 AR

List cannot hold primitive values because of java generics (see similar question). So when you call Arrays.asList(ar) the Arrays creates a list with exactly one item - the int array ar.

编辑:

Arrays.asList(AR)将是列表与LT结果; INT []&GT; ,<强>不可以 列表&LT; INT&GT; 键,将举办一个项目也就是 INT 在数组:

Result of Arrays.asList(ar) will be a List<int[]>, NOT List<int> and it will hold one item which is the array of ints:

[ [1,2,3,4,5] ]

您不能从列表本身访问原始的 INT 秒。你必须访问它是这样的:

You cannot access the primitive ints from the list itself. You would have to access it like this:

list.get(0).get(0) // returns 1
list.get(0).get(1) // returns 2
...

和我想这不是你想要的。

And I think that's not what you wanted.

这篇关于使用Arrays.asList与int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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