创建List集合

1
2
3
4
5
List<String> stringList = new ArrayList<>();
stringList.add("1");
stringList.add("2");
stringList.add("3");
stringList.add("4");

遍历

for循环

1
2
3
for (int i = 0; i < stringList.size(); i++) {
System.out.print(stringList.get(i)+" ");
}

foreach循环

1
2
3
for (String s : stringList) {
System.out.print(s+" ");
}

迭代器

1
2
3
4
Iterator iterator = stringList.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}