I need to access the current iterator object when iterating through a list. Currently, I only know how to use the hasNext() and next() methods respectively to check whether the list has a next element and to access the next object. But, is there a way that I can access the current object?
List<Game> games = new ArrayList<>(Game);
Iterator<Game> myIterator = games.iterator();
while(myIterator.hasNext){
//Here, I need to access the current element
//I can only use .next() which I think is throwing
//an exception
myIterator.next();
//what if there is only one element in the list,
//The .next() could move to the null pointer
}
Aucun commentaire:
Enregistrer un commentaire