java的:基本类型谅解Arrays.asList(T ...数组)方法 [英] java : Understanding Arrays.asList(T...array) method for primitive types

查看:182
本文介绍了java的:基本类型谅解Arrays.asList(T ...数组)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写以下code和惊讶地看到输出:

 整数= 211;
    INT B = 211;    INT []数组= {} 210211212;    的System.out.println(Arrays.asList(阵列)。载有(一));
    的System.out.println(Arrays.asList(阵列)。载有(B));

输出:

 

我发现<一个href=\"http://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java\">this问题链接到它,得知 asList 方法不Autobox东西一些其他问题。我检查了的Eclipse Javadoc preVIEW返回类型:

我也不太明白返回类型。 INT [] 是一个对象,而不是原始所以它的罚款。我敢肯定,我没有收到列表&LT;整数GT; (这一点我预期),但我不知道如何使用它返回的东西。我的问题是:

    1。究竟如何我希望清单方法将工作时,我期待整数的名单并获得INT []的列表?
    2。在字符串的情况下,返回类型为String的列表,而不是字符串[]列表。什么样的实现差异有哪些?
    3。有什么好处是这种方法的原语,如果事情是如此不确定?

解决方案

有明显的3个问题在这里所以让我们解决这些问题一个接一个:


  

      
  1. 究竟如何我希望清单方法将工作时,我期待整数的名单并获得INT []?
  2. 的List
      

好吧,列表方法,将工作完全符合市场预期,一个列表&LT; T&GT; 是一个类型列表 T 。在这里, T 是<为code> INT [] 所以列表&LT; INT []&GT; 将包含数组作为每个元素:

  [{1,2},{3,4},{1,6}]

所以 GET(我)将返回第i 元素。在的情况下, Arrays.asList 列表包含一个元素,即 INT [ ] 这样:

  INT []数组= {} 210211212;
清单&LT; INT [] GT;清单= Arrays.asList(数组);

  [{210,211,212}]

所以

  list.get(0)[0] == 210


  

在字符串的情况下,返回类型为String的列表和字符串不是列表[]。什么样的实现差异有哪些?


字符串对象不可以基本类型。所不同的遵循从。


  <醇开始=3>
  
  • 有什么好处是这种方法的原语,如果事情是如此不确定?

  •   

    事情没有的不确定的。这种方法导致的确定 predictable 的行为。它只是不适合原语非常有用的。这是(另一个)Java的类型系统的仿制药相结合的副作用。

    请注意与Java 8转换的 INT [] 列表&LT;整数GT; 很简单:

     列表&LT;整数GT;清单= Arrays.stream(阵列)。
            盒装()。
            收集(了ToList());

    I wrote following code and was surprised to see the output:

        Integer a = 211;
        int b = 211;
    
        int[] array = {210,211,212};
    
        System.out.println(Arrays.asList(array).contains(a));
        System.out.println(Arrays.asList(array).contains(b));
    

    Output:

    false
    false
    

    I found this question and some other questions linked to it and learned that asList method doesn't Autobox stuffs. I checked the returned type in eclipse javadoc preview:

    I couldn't quite understand this return type. int[] is an object and not a primitive so its fine. I'm sure that I'm not getting List<Integer> (something which I expected) but I'm not sure how to use the thing which is being returned. My questions are:

      1. How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?

      2. In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?

      3. What good is this method for primitives if things are so uncertain?

    解决方案

    There are obviously 3 questions here so lets tackle them one by one:

    1. How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?

    Well, List methods will work exactly as expected, a List<T> is a list of types T. Here T is an int[] so a List<int[]> will contains arrays as each element:

    [{1, 2}, {3, 4}, {1, 6}]
    

    So get(i) will return the ith element. In the case of Arrays.asList the List contains a single element, namely the int[] so:

    int[] array = {210,211,212};
    List<int[]> list = Arrays.asList(array);
    

    Will be

    [{210, 211, 212}]
    

    And so

    list.get(0)[0] == 210
    

    In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?

    String is an Object, not a primitive type. The difference follows from that.

    1. What good is this method for primitives if things are so uncertain?

    Things are not uncertain. This method results in defined and predictable behaviour. It's just not very useful for primitives. This is (yet another) side effect of combining Java's type system with generics.

    Note with Java 8 the conversion of an int[] to a List<Integer> is very simple:

    List<Integer> list = Arrays.stream(array).
            boxed().
            collect(toList());
    

    这篇关于java的:基本类型谅解Arrays.asList(T ...数组)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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