| Author |
Guess the answer and please explain
|
Abdul Mohsin
Ranch Hand
Joined: Apr 26, 2007
Posts: 111
|
|
int[] str={2,3,4,5}; List list=Arrays.asList(str); System.out.println(list.size()); Regards, Abdul Mohsin
|
Regards, Abdul Mohsin
|
 |
Abdul Mohsin
Ranch Hand
Joined: Apr 26, 2007
Posts: 111
|
|
Hi, answer is 1 but can anybody explain me the reason . Regards, Abdul Mohsin
|
 |
John Stone
Ranch Hand
Joined: May 04, 2007
Posts: 332
|
|
because asList(T...a) calls Arrays.ArrayList(E[] array), and in generics only Object subtypes are allowed as type parameters. Your array, is in fact processed as a single object of type "Object", wrapped to one dimensional array :-). try this code: gives 4
|
 |
John Stone
Ranch Hand
Joined: May 04, 2007
Posts: 332
|
|
accessing your array if we use int[]
|
 |
Abdul Mohsin
Ranch Hand
Joined: Apr 26, 2007
Posts: 111
|
|
Thanks John, I got it . Regards, Abdul Mohsin
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Is this: not how it is meant to be used?
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Abdul Mohsin
Ranch Hand
Joined: Apr 26, 2007
Posts: 111
|
|
Hi John, I understood that but Can you explain it a bit more that what happens when we pass int array and String array to generic vararg method . How these two are considered differently , int array as one object and String array as normal array. Regards, Abdul Mohsin
|
 |
Sergio Tridente
Ranch Hand
Joined: Mar 22, 2007
Posts: 329
|
|
This line won't compile:
Originally posted by Barry Gaunt: List<Integer> list=Arrays.asList(1,2,3,4);
It'll give: "cannot convert from List<int[]> to List<Integer>" [ May 21, 2007: Message edited by: Sergio Tridente ]
|
SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
|
 |
John Stone
Ranch Hand
Joined: May 04, 2007
Posts: 332
|
|
Originally posted by Sergio Tridente: This line won't compile: It'll give: "cannot convert from List<int[]> to List<Integer>" [ May 21, 2007: Message edited by: Sergio Tridente ]
Compiles & run fine with java 1.5.
|
 |
Sergio Tridente
Ranch Hand
Joined: Mar 22, 2007
Posts: 329
|
|
Originally posted by John Stone: Compiles & run fine with java 1.5.
No, it doesn't. The following code: is giving me the following error when I try to compile it:
Can you try to compile the very same code andpost results? Thank you.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
For me it compiles and runs producing: 1 2 3 4 4 But I am using the Java 6.0 jdk - perhaps there's a diffence with the Java 5.0 jdk. Nope - it works under Java 5.0 too. [ May 21, 2007: Message edited by: Barry Gaunt ]
|
 |
Marcos R Oliveira
Ranch Hand
Joined: Apr 20, 2002
Posts: 51
|
|
Sergio, Your code doesn�t compile, Barry�s does. In Barry�s code: each item in the asList() method is autoboxed to an Integer what gives an array of four Integers. In your code: you are trying to convert List<int[]> to List<Integer>. Marcos
|
SCJP 1.4 - SCJP 1.6 - SCWCD in progress
|
 |
Abdul Mohsin
Ranch Hand
Joined: Apr 26, 2007
Posts: 111
|
|
it will compile and run fine in 1.5 Regards, Abdul Mohsin
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
Yeah. It would help if he ran the same code as me :roll:
|
 |
Sergio Tridente
Ranch Hand
Joined: Mar 22, 2007
Posts: 329
|
|
Sorry, my mistake. I didn't realise that Barry's code was using varargs. I tested it and it worked fine. Thanks. However, now I don't understand why those integers get autoboxed when varargs are involved. Does anyone have an answer? Thank you again.
|
 |
 |
|
|
subject: Guess the answer and please explain
|
|
|