• 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

converting Arrays to List?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic