- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- public class Test {
- public static void main(String[] args) {
- List list = new ArrayList();
- list.add(3);
- list.add(2);
- list.add(10);
- list.add(4);
- list.add(70);
- /* System.out.println("---方法一 -----");
- for(Iterator iterator = list.iterator();iterator.hasNext();){
- int i = (Integer) iterator.next();
- System.out.println(i);
- }
- /* System.out.println("---方法二 -----");
- Iterator iterator = list.iterator();
- while(iterator.hasNext()){
- int i = (Integer) iterator.next();
- System.out.println(i);
- }
- System.out.println("---方法三 -----");
- for (Object object : list) {
- System.out.println(object);
- }*/
- System.out.println("---方法四 -----");
- for (int i = 0 ;i<list.size();i++) {
- int j= (Integer) list.get(i);
- System.out.println(j);
- }
- }
- }
Java遍历集合四种方法
最新推荐文章于 2023-07-10 19:56:16 发布