[Java]Iteratorでループ処理を行う方法
書式
public interface Iterator<E>
hasNext() :反復処理でさらに要素がある場合にtrueを返します。
使用例
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
// リストの作成
ArrayList<String> eleLst = new ArrayList<String>();
eleLst.add("A01");
eleLst.add("B02");
eleLst.add("C03");
eleLst.add("D04");
// イテレータシュウトク
Iterator<String> it = eleLst.iterator();
String res;
//要素を出力
while(it.hasNext()) {
res =(String)it.next();
System.out.println(res);
}
}
}
結果
A01
B02
C03
D04