• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Guess the answer and please explain

 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] str={2,3,4,5};

List list=Arrays.asList(str);

System.out.println(list.size());

Regards,

Abdul Mohsin
 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

answer is 1

but can anybody explain me the reason .

Regards,

Abdul Mohsin
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
accessing your array if we use int[]

 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John,

I got it .

Regards,

Abdul Mohsin
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this:


not how it is meant to be used?
 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


sergio@linux:~> javac ArraysTest.java
ArraysTest.java:7: incompatible types
found : java.util.List<int[]>
required: java.util.List<java.lang.Integer>
List<Integer> list2=Arrays.asList(str2);
^
1 error



Can you try to compile the very same code andpost results? Thank you.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it will compile and run fine in 1.5

Regards,

Abdul Mohsin
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. It would help if he ran the same code as me :roll:
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic