遍历List集合的方式有很多,现在举出最常见的三种

        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");

        //方法一: 用for循环, 以size为条件遍历
        for(int i = 0 ; i < list.size() ; i++) {
          System.out.print("for循环:"+list.get(i));
          System.out.println();
        }
        //方法二:增强型for循环遍历
        for(String attribute : list) {
          System.out.print("增强型for:"+attribute);
          System.out.println();
        }

        //方法三:用迭代器迭代
        Iterator it = list.iterator();
        while(it.hasNext()) {
          System.out.print("用迭代器迭代:"+it.next());
          System.out.println();
        }

控制台打印如下:
遍历List集合的方式

 

相关文章:

  • 2022-02-01
  • 2021-09-20
  • 2021-12-31
  • 2021-04-20
  • 2021-12-31
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-31
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2021-12-31
  • 2021-10-01
  • 2021-12-31
相关资源
相似解决方案