package collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
//list的迭代方法
public class TestCollection {
public static void main(String[] args) {
List<Hero> heroes = new ArrayList<>();
for (int i = 0; i < 100; i++) {
heroes.add(new Hero("hero " + i));
}
System.out.println(heroes);
int j = 0;
for (int i = 0; i < heroes.size(); i++) {
if (i > 0 && i % 8 == 0) {
j++;
heroes.remove(i-j+1);
}
}
//通过list的iterator方法得到Iterator对象
Iterator<Hero> iterator = heroes.iterator();
while (iterator.hasNext()) {
Hero h = iterator.next();
System.out.print(h + " ");
}
System.out.println("");
System.out.println("----------------------->");
for (Iterator<Hero> iterator1 = heroes.iterator(); iterator1.hasNext();) {
Hero h = iterator1.next();
System.out.print(h + " ");
}
}
}
練習 2017-08-22 list的迭代方式
最新推荐文章于 2024-03-25 21:29:21 发布