File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java 1.5  typed collections..." Watch "Java 1.5  typed collections..." New topic

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 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)
I agree. Here's the link:
subject: Java 1.5 typed collections...
It's not a secret anymore!