Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why the list size is 1

 
amit patidar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

public static void main(String args[]){

int[] primArray = new int[3];

primArray[0] =2;
primArray[1] =3;
primArray[2] =5;

List list = Arrays.asList(primArray);
System.out.println(list.size());


}

The above code compiles fine and gives the list size as 1 , why not 3.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the API documentation for the method Arrays.asList()?

public static <T> List<T> asList(T... a)

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.
This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

Especially note the argument type of the method and the example.

What do you think the single element in the list that you get contains?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, see Bug ID 6353471 in Sun's bug database.
Not that this is not a bug, but the Bug DB entry is nonetheless relevant.
 
amit patidar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the reason

when we pass primitive array to Arrays.asList() method , than the list.get(0) with return the complete passed primitive array
but when we pass array of objects let say Integer[] then it converts

Integer[0] equivalent to list.get(0)

Integer[1] equivalent to list.get(1)

.
.
.
.
.
Integer[n] equivalent to list.get(n)

in this manner
 
Campbell Ritchie
Sheriff
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What an interesting problem. I tried it out:
campbell@queeg:~/java$ java ListSizeDemo
Passing the object array gives a 0-member class java.util.Arrays$ArrayList-type list
Passing the primitive array gives a 1-member class java.util.Arrays$ArrayList-type list
[2]+ Done javac ListSizeDemo.java
campbell@queeg:~/java$ java ListSizeDemo 123 234 345 456
Passing the object array gives a 4-member class java.util.Arrays$ArrayList-type list
Passing the primitive array gives a 1-member class java.util.Arrays$ArrayList-type list
I can only presume it is because an int is not an object and can therefore not be a "T". When you pass an Integer[] array, the JVM interprets that as T = Integer, and fits the array into the ... Remember ... is a shorthand way of creating an array. When you pass an int[], the JVM cannot interpret T = int, so it has to interpret T = int[]. Note when I passed no arguments, the JVM created a 0-size array; when the asList method received no Integers, it created a 0-sized array, but when it receives a 0-member int[] array, it sees that as a single element.

I knew you could pass several elements to ... and they would turn into an array, but didn't realise you could pass a pre-existing array directly. But remember you cannot have [] and ... as the only difference in method signature; the compiler will interpret them as the same signature and fail to compile as a duplicate declaration.
It also shows that you cannot force a primitive into any generic type. Also note that because of type erasure, printing the class types of the List gives us no helpful information at all.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I thought appears to be similar to the explanation in the llnk Jelle Klap posted.
 
amit patidar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got my answer , so now the thread is closed

Thank you Campbell.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic