Java 中的迭代器
Java 中的集合框架中使用迭代器来逐个检索元素。Java 中有三个迭代器
- 列举
- 迭代程序
- 列表迭代器
1.列举
这是一个用于获取遗留集合(矢量,哈希表)元素的接口。枚举是 JDK 1.0 中出现的第一个迭代器,休止符包含在 JDK 1.2 中,功能更多。枚举也用于指定序列输入流的输入流。我们可以通过在任何向量对象 上调用向量类的元素()方法来创建枚举对象
// Here "v" is an Vector class object. e is of
// type Enumeration interface and refers to "v"
Enumeration e = v.elements();
枚举界面有两种方法,即:
// Tests if this enumeration contains more elements
public boolean hasMoreElements();
// Returns the next element of this enumeration
// It throws NoSuchElementException
// if no more element present
public Object nextElement();
Java 语言(一种计算机语言,尤用于创建网站)
// Java program to demonstrate Enumeration
// Importing Enumeration and Vector classes
// from java.util package
import java.util.Enumeration;
import java.util.Vector;
// Main class
public class Test
{
// Main driver method
public static void main(String[] args)
{
// Creating a vector object
Vector v = new Vector();
// Iterating over vector object
for (int i = 0; i < 10; i++)
v.addElement(i);
// Printing elements in vector object
System.out.println(v);
// At beginning e(cursor) will point to
// index just before the first element in v
Enumeration e = v.elements();
// Checking the next element availability where
// condition holds true till threre is a single element
// remaining in the List
while (e.hasMoreElements())
{
// Moving cursor to next element
int i = (Integer)e.nextElement();
// Print above elements in object
System.out.print(i + " ");
}
}
}
版权属于:月萌API www.moonapi.com,转载请注明出处