迭代器是一个接口,用于逐个获取集合中的元素。它位于名为 Java 的 Java 包中。实用程序包。集合 API 实现了 iterator() 方法,因此可以从 Map、List、Queue、Deque 和 Set 等接口检索数据,这些接口都是集合框架实现的。顾名思义,Java 中的迭代器会迭代对象的集合。
语法:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Iterator<E> iterator()
迭代器下面是调用集合接口的 iterator() 方法创建的对象的名称。 “collection”是集合对象的名称。
Iterator iter = collection.iterator();
Java 中的迭代器有 4 个方法,用于遍历集合并检索所需的信息。它们如下:
下面是Java中Iterator的例子:
代码:
import java.io.*; import java.util.*; public class IteratorExample { public static void main(String[] args) { ArrayList<String> val = new ArrayList<String>(); val.add("Iteration started"); val.add("Printing iteration1"); val.add("Printing iteration2"); val.add("Printing iteration3"); val.add("End of iteration"); // Iterates through the list Iterator iter = val.iterator(); System.out.println("The values of iteration are as follows: "); while (iter.hasNext()) System.out.println(iter.next() + " "); System.out.println(); } }
输出:
在元素列表中,迭代器只能获取现有元素的信息。因此,如果尝试访问下一次迭代中不存在的元素,它将崩溃或引发异常。在这里我们将了解在实现迭代器方法时遇到的不同类型的异常。
迭代一组元素并通过此方法获取它们时。
这里可能发生两种异常:
语法:
//ArrayList is created from the list having fixed size list = new ArrayList<String>(list); Iterator<String> iter = list.iterator(); while(iter.hasNext()){ if( iter.next().equals("First iteration") ){ iter.remove(); } }
这些方法允许迭代器在集合对象的两个方向上进行遍历。以下是其中一些:
Below is an example in ArrayList for ListIterator.
Code:
import java.util.*; public class IteratorExample { public static void main(String args[]) { // Creating an array list ArrayList array = new ArrayList(); // add elements to the array list array.add("First element"); array.add("Second element"); array.add("Third element"); array.add("Fourth element"); array.add("Fifth element"); array.add("Sixth element"); // Displaying elements of an array System.out.println("Printing input of the array: "); Iterator iter = array.iterator(); while(iter.hasNext()) { Object value = iter.next(); System.out.println(value + " "); } System.out.println(); // To update the elements of iteration ListIterator listiter = array.listIterator(); while(listiter.hasNext()) { Object value = listiter.next(); listiter.set(value + "+"); } System.out.print("Updated array elements are as follows: "); iter = array.iterator(); while(iter.hasNext()) { Object value = iter.next(); System.out.print(value + " "); } System.out.println("\n"); // To display the contents in backward direction System.out.println("Printing elements in backward direction: "); while(listiter.hasPrevious()) { Object value = listiter.previous(); System.out.print(value + " "); } System.out.println(); } }
Output:
Below are the advantages of Iterators:
Below are the limitations of Iterators:
Iterators are the most commonly used method to retrieve elements from the collection interface. It is called Universal Java Cursor as it is applicable across all the Collection classes.
This is a guide to Iterator in Java. Here we discuss methods and examples of Iterator in Java along with its Limitations and Advantages. You can also go through our other suggested articles to learn more –
以上是Java 中的迭代器的详细内容。更多信息请关注PHP中文网其他相关文章!