2

According to basics
-->int[] is array of primitive
-->and Integer[] is object array

But I am not able to understand the behavior when you convert both into List. Please refer the example to understand the question

scenerio 1:

int [] a1={2,10,55,60};

    /** To covert it into List

     **/
    List list  = (Arrays.asList(a1));
    System.out.println("size :"+list.size());
    Iterator it =list.iterator();
    while (it.hasNext()){
        System.out.println(it.next());
    }

the out put for the above code is size :1 [I@f74f6ef(Address )

scenerio 2:

     Integer [] a1={2,10,55,60};

    /** To covert it into List

     **/
    List list  = (Arrays.asList(a1));
    System.out.println("size :"+list.size());
    Iterator it =list.iterator();
    while (it.hasNext()){
        System.out.println(it.next());
    }

the out put for the above code is size :4
2 10 55 60

Now one reason could be that the a1(int) might be holding all the elements of the array with address as Address+4(int) for consecutive elements. and whereas a1 (Integer) would be holding the address of each and every element.
so might be reason that the size in case of int is 1 and in Integer its 4. I am not sure with the above statement. can some one please help me to understand the situation??

5
  • Don't use raw types. If you do List<Integer> list = (Arrays.asList(a1)); the compiler will give you an incompatible types error which partially explains it.
    – Radiodef
    Jun 13, 2015 at 15:46
  • Hi Radiodef Please share me the link of the original question. So that I can clear my doubt Jun 13, 2015 at 15:46
  • 1
    The duplicate link is at the top of the question body. (Refresh the page.) The answer is that Arrays.asList(a1) creates a List<int[]>.
    – Radiodef
    Jun 13, 2015 at 15:47
  • @vinodbazari It's at the top of the question, in a yellow box. Jun 13, 2015 at 15:47
  • @Radiodef : thnx ..... Jun 13, 2015 at 16:04

0

Browse other questions tagged or ask your own question.