List有三种循环方式:
分别是经典的for循环,迭代器(list.iterator()),foreache循环(和C#相同哦)
测试代码如下:
public static void main(String[] args) { |
List<String> list= new ArrayList<String>(); |
list.add( "123" ); |
list.add( "456" ); |
list.add( "789" ); |
System.out.println( "------经典的for循环-----" ); |
for ( int i = 0 ; i < list.size(); i++) { |
System.out.println(list.get(i)); |
} |
System.out.println( "-------Iterate循环--------" ); |
Iterator<String> iter=list.iterator(); |
while (iter.hasNext()) |
{ |
System.out.println(iter.next()); |
} |
System.out.println( "-------foreach循环---------" ); |
for (String string : list) { |
System.out.println(string); |
} |
} |