Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
151 views
in JAVA by (178k points)
How do you use an Iterator in Java?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

To use an Iterator in Java, you need to follow these basic steps:

1. Obtain an instance of the Iterator interface by calling the iterator() method on a Collection object. 

For example:

List<String> list = new ArrayList<String>();
list.add("element 1");
list.add("element 2");
list.add("element 3");

Iterator<String> iterator = list.iterator();
 

In this example, we create an ArrayList of strings, add three elements to it, and obtain an Iterator object to iterate over the elements in the list.

2. Loop through the collection using the hasNext() and next() methods of the Iterator interface. The hasNext() method returns true if there are more elements in the collection, and the next() method returns the next element in the collection. 

For example:

while (iterator.hasNext()) {
    String element = iterator.next();
    System.out.println(element);
}
 

This code will loop through the collection and print each element to the console.

3. Optionally, remove elements from the collection using the remove() method of the Iterator interface. This method removes the last element returned by the iterator from the underlying collection. 

For example:

while (iterator.hasNext()) {
    String element = iterator.next();
    if (element.equals("element 2")) {
        iterator.remove();
    }
}
 

This code will remove the second element from the collection.

Note that the Iterator interface is used to access elements of a collection without exposing the underlying data structure. This means that you can use an iterator to loop through elements of any type of collection, such as ArrayList, LinkedList, HashSet, etc.

Here's a complete example that demonstrates how to use an Iterator to loop through an ArrayList of integers:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IteratorExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<Integer>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        Iterator<Integer> iterator = numbers.iterator();
        while (iterator.hasNext()) {
            int number = iterator.next();
            System.out.println(number);
        }
    }
}
 

This code will loop through the ArrayList of integers and print each number to the console.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Apr 25, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked Apr 25, 2023 in JAVA by kvdevika (178k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...