wood burning stoves 2.0*
The moose likes Java in General and the fly likes why the list size is 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why the list size is 1" Watch "why the list size is 1" New topic
Author

why the list size is 1

amit patidar
Greenhorn

Joined: Jul 10, 2009
Posts: 12
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

Joined: Aug 16, 2005
Posts: 14114
    
  16

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
amit patidar
Greenhorn

Joined: Jul 10, 2009
Posts: 12
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

Joined: Oct 13, 2005
Posts: 38441
    
  23
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

Joined: Oct 13, 2005
Posts: 38441
    
  23
What I thought appears to be similar to the explanation in the llnk Jelle Klap posted.
amit patidar
Greenhorn

Joined: Jul 10, 2009
Posts: 12
I got my answer , so now the thread is closed

Thank you Campbell.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why the list size is 1