当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。