Forums Register Login

why the list size is 1

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
What I thought appears to be similar to the explanation in the llnk Jelle Klap posted.
+Pie Number of slices to send: Send
I got my answer , so now the thread is closed

Thank you Campbell.

I knew I would regret that burrito. But this tiny ad has never caused regrets:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1511 times.
Similar Threads
running time of ternary search
Behavior of Arrays.asList()
EL Coercion question
Unable to understand toArray()
Why I get the List content when I print the List object?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:53:41.