티스토리 뷰


// Java : Iterator

package iteration;

import java.util.LinkedList;
import java.util.Iterator;

public class Program {

public static void main(String[] args) {
LinkedList<String> animals = new LinkedList<String>();
animals.add("dog");
animals.add("cat");
animals.add("fox");

for (String animal : animals) {
System.out.println(animal);
}

// Iterator class
// hasNext(): Returns true if there is at least one more element;
// otherwise, it returns false.
// next(): Returns the next object and advances the iterator.
// remove(): Removes the last object that was returned by next from
// the collection.
Iterator<String> it = animals.iterator();
while (it.hasNext()) {
String animal = it.next();
System.out.println(animal);
}
}
}


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함