Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-02-2007, 01:02 PM   #1
Member (4 bit)
 
Join Date: Jun 2006
Posts: 12
Java iterators and casting

Suppose i have the following code:
Code:
for ( Iterator it = myList.iterator(); it.hasNext(); )
       T element = it.next();
The IntelliJ IDE tells me it is replaceable by for each loop. When i tell it to replace
it produces the following code:
Code:
for ( T t :(Iterable) myList )
        T element = t;
But then it says:
Unchecked cast: MyCollection to java.lang.Iterable
Can i overcome this unchecked cast somehow?

myList.iterator() function returns Iterator object btw.
Lan4ear is offline   Reply With Quote
Old 05-02-2007, 06:49 PM   #2
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
A for-loop is primarily utilized incrementing/decrementing integers. If I recall, the Iterator is best used in a while loop.
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
Force Flow is offline   Reply With Quote
Old 05-06-2007, 09:48 PM   #3
Member (7 bit)
 
Join Date: Aug 2003
Location: Hinckley in Leicestershire
Posts: 110
I'm fairly new to Java so this may be totally wrong, but have you implemented the iterable interface?

So..
Code:
public class MyClassName implements Iterable
Then just take out the casting part
Code:
for ( T t : myList )
        T element = t;
Like I say that may be totally wrong as I'm new to Java.
Tiestarian is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:28 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2