aspose file tools*
The moose likes Beginning Java and the fly likes converting Arrays to List? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting Arrays to List?" Watch "converting Arrays to List?" New topic
Author

converting Arrays to List?

Avander Be
Greenhorn

Joined: Oct 16, 2007
Posts: 16
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!


Steve Jobs: "Nobody uses Java anymore" ( Who the f*ck is Steve Jobs?)
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

The List can be found in java.util.List. You have import java.util.*, that's why you can use the List.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Shilpa
Greenhorn

Joined: May 10, 2006
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Avander Be
Greenhorn

Joined: Oct 16, 2007
Posts: 16
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

You got it.
 
 
subject: converting Arrays to List?