wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java 1.5  typed collections... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java 1.5  typed collections..." Watch "Java 1.5  typed collections..." New topic
Author

Java 1.5 typed collections...

Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
If I make the following declaration:



then later do the following


Is that legal? does the iterator automatically return an object of the type specified in the angle brackets? if not is this just an oversight or was there a compelling reason for not typing the iterator to what was specified in the collection/list? It seems that its necessary to cast the object coming out of the iterator...

or should I be saying for(Iterator<String> = list.iterator....??

-Tad
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
Ok I guess I just need to ask the question out loud(er type it out) to figure it out... heh.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
You got it.

Side note. Rather than this:

ArrayList<String> list = new ArrayList<String>()

Do this:

List<String> list = new ArrayList<String>()

And this:

for(String string : list)
{
...
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java 1.5 typed collections...
 
Similar Threads
ArrayList with Iterator
tiger - doubt in this code ...
how to print only strings using generics when the list is having multiple types
For-eaching an iterator
Problem retrieving arraylist data in jsp page.Help needed :)