• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

John Meyers Mock exam

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer is:
// Fragment 1
List <Integer> l = Arrays.asList( new int[] {1,2,3,4} );


// Fragment 2
List <Object> list = new List <Object> ();

Both fragments will not compile. The asList() method takes a type T as an argument so int[] does not fit that description. You cannot instantiate the interface List.

I don't understand the explanation given for the compilation failure of fragment1( The asList() method takes a type T as an argument so int[] does not fit that description)

Could someone explain me please ?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generic types are objects. It is expecting an array of Type T (which has to be an object type). Type T can't be an int, because ints is a primitive type.

Henry
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Henry,

I tried to create an array of Integer, but it doesnt work either. why ?

Integer[] ar = {1,2,3,4};
List<Integer> l = Arrays.asList(ar);
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried to create an array of Integer, but it doesnt work either. why ?



It works fine for me. What is the error that you are getting?

Henry
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh thanks, I forgot to import java.util.*

sorry
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aarggh ! Henry beat me to it

His explanation is quite to the point.

Good luck with your preparation Mamadou
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic