java.util.ArrayList.iterator()でListの要素を取り出す
Javaコード
package sample;
import java.util.ArrayList;
import java.util.Iterator;
public class ListAddStrDemo {
public static void main(String[] args) {
ArrayList<String> cft = new ArrayList<String>();
cft.add(“test–11");
cft.add(“test–22");
cft.add(“test–33");
cft.add(“test–44");
cft.add(“test–55");
cft.add(null);
cft.add(3, “test-66");
System.out.println(“*****Iteratorメソッド*****");
// Iterator文
Iterator<String> it = cft.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println(“\r\n—-forメソッド—“);
// for文
for (String kd : cft) {
System.out.println(kd);
}
}
}
結果
*****Iteratorメソッド*****
test–11
test–22
test–33
test-66
test–44
test–55
null
—-forメソッド—
test–11
test–22
test–33
test-66
test–44
test–55
null