Java AbstractSequentialList |迭代器()方法
Java中的AbstractSequentialList iterator()方法用于获取列表上的迭代器或列表迭代器。它返回此列表中元素的迭代器(以正确的顺序)。此方法覆盖类 AbstractList
句法:
public Iterator iterator()
返回值:此方法返回此列表中元素的迭代器(以正确的顺序)。
下面是说明 iterator() 方法的代码:
方案一:
// 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 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 Itr = absl.iterator();
// Traversing elements with help of iterator
while (Itr.hasNext()) {
System.out.print(Itr.next() + " ");
}
}
}
输出:
5 9 6 7 10 8 7
方案二:
// 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 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 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