Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting Arrays to List?

 
Avander Be
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code is based on code from K&B SCJP Study Guide p. 559.



I had to add the import and the <String> to List in order to compile.

What puzsles me is where did that List type come from?? The only List type I found in the Java doc is an... interface!
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The List can be found in java.util.List. You have import java.util.*, that's why you can use the List.
 
Shilpa
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays.asList(..) has changed from
public static List asList(Object a)
to (Java 1.5)
public static <T> List<T> asList(T... a)

Shilpa
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Avander Be:

What puzsles me is where did that List type come from?? The only List type I found in the Java doc is an... interface!

You must have overlooked java.awt.List, but that's a GUI element

It's right that java.util.List is an interface. The Arrays class has an internal class that implements List and wraps arrays. An instance of this class is instantiated when you call Arrays.asList. You don't need to know the full name of the class, only that it implements List, and you cannot add or remove elements. All other List methods can be called without a problem.
 
Avander Be
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

You must have overlooked java.awt.List, but that's a GUI element

Yep, I did see it but figured out it was kinda 'out of scope' here :-)).

Originally posted by Rob Prime:
It's right that java.util.List is an interface. The Arrays class has an internal class that implements List and wraps arrays. An instance of this class is instantiated when you call Arrays.asList. You don't need to know the full name of the class, only that it implements List, and you cannot add or remove elements. All other List methods can be called without a problem.

It's just another 'polymorphism' trick isn't it?
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic