當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java AbstractSequentialList iterator()用法及代碼示例


Java中的AbstractSequentialList iterator()方法用於在列表上獲取Iterator或List Iterator。它返回此列表中元素的迭代器(按適當順序)。此方法重寫類AbstractList <E>中的iterator()

用法:

public Iterator<E> iterator()

返回值:此方法返回此列表中元素的迭代器(按適當順序)。


下麵的代碼說明了iterator()方法:

程序1:

// Java program to demonstrate 
// iterator() method 
  
import java.util.*; 
  
public class GfG { 
  
    public static void main(String[] args) 
    { 
        // Creating an instance of the AbstractSequentialList 
        AbstractSequentialList<Integer> absl = new LinkedList<>(); 
  
        // adding elements to absl 
        absl.add(5); 
        absl.add(6); 
        absl.add(7); 
        absl.add(2, 8); 
        absl.add(2, 7); 
        absl.add(1, 9); 
        absl.add(4, 10); 
  
        // Getting Iterator 
        Iterator<Integer> Itr = absl.iterator(); 
  
        // Traversing elements with help of iterator 
        while (Itr.hasNext()) { 
            System.out.print(Itr.next() + " "); 
        } 
    } 
}
輸出:
5 9 6 7 10 8 7

程序2:

// Java program to demonstrate 
// iterator() method 
  
import java.util.*; 
  
public class GfG { 
  
    public static void main(String[] args) 
    { 
        // Creating an instance of the AbstractSequentialList 
        AbstractSequentialList<String> absl = new LinkedList<>(); 
  
        // adding elements to absl 
        absl.add("Geeks"); 
        absl.add("ForGeeks"); 
        absl.add("A"); 
        absl.add("Portal"); 
        absl.add(3, "Computer Science"); 
        absl.add(5, "For Geeks"); 
  
        // Getting Iterator 
        Iterator<String> Itr = absl.iterator(); 
  
        // Traversing elements with help of iterator 
        while (Itr.hasNext()) { 
            System.out.println(Itr.next()); 
        } 
    } 
}
輸出:
Geeks
ForGeeks
A
Computer Science
Portal
For Geeks


相關用法


注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Java AbstractSequentialList | iterator() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。