Maggie Maggie - 3 months ago 21
Java Question

Isn't iterator a built-in function, why do I have to import it?

// inside my class
public void playRound() {
Iterator<Player> itr = players.iterator();
Player player;;
// Supply this code!

It says :Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Iterator cannot be resolved to a type

Isn't Iterator a built in function?

I am forced to import :

import java.util.Iterator;

If I want to resolve this issue, is there a way to avoid importing the Iterator.
The reason why I cannot simply import the Iterator even though it would save me a lot of time, is because for my project I am not allowed to import anything other than import java.util.ArrayList;

Furthermore, I am using eclipse to write my code.


In a word - no. There's nothing "magical" about an Iterator. It's a class from the java.util package, and if you want to use it you should either import it or reference its fully qualified name:

java.util.Iterator<Player> itr = players.iterator();

But I guess this is also forbidden by your requirements. Instead, you could just use an enhanced for loop:

for (Player player : players) {;