File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

John Meyers Mock exam

 
Mamadou Touré
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • 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 ?
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh thanks, I forgot to import java.util.*

sorry
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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 is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic