java - Isn't iterator a built-in function, why do I have to import it? -
// inside class public void playround() { iterator<player> itr = players.iterator(); while(itr.hasnext()){ player player =itr.next(); player.play(par); } // supply code! }
it says :exception in thread "main" java.lang.error: unresolved compilation problem: iterator cannot resolved type
isn't iterator built in function?
i forced import :
import java.util.iterator;
if want resolve issue, there way avoid importing iterator. reason why cannot import iterator though save me lot of time, because project not allowed import other import java.util.arraylist;
furthermore, using eclipse write code.
in word - no. there's nothing "magical" iterator
. it's class java.util
package, , if want use should either import or reference qualified name:
java.util.iterator<player> itr = players.iterator();
but guess forbidden requirements. instead, use enhanced for
loop:
for (player player : players) { player.play(par); }
edit:
@yshavit noted in comments, since players
list
, not old iterable
, can access elements directly via index, meaning use "old fashioned" for
loop:
for (int = 0; < players.size(); ++i) { players.get(i).play(par); }
Comments
Post a Comment