迭代器是一個接口,用來逐一取得集合中的元素。它位於名為 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中文網其他相關文章!