TangGe520

如何遍历List对象

for(String str : list) {//其内部实质上还是调用了迭代器遍历方式,这种循环方式还有其他限制,不建议使用。
    System.out.println(str);
}

.普通for循环

for( int i = 0 ; i < list.size() ; i++) {//内部不锁定,效率最高,但在多线程要考虑并发操作的问题。
    System.out.println(list.get(i));
}

迭代器遍历

Iterator<String> iter = list.iterator();
while(iter.hasNext()){  //执行过程中会执行数据锁定,性能稍差,若在循环过程中要去掉某个元素只能调用iter.remove()方法。
    System.out.println(iter.next());
}

 

 

分类:

技术点:

相关文章:

  • 2021-12-31
  • 2021-11-08
  • 2021-11-03
  • 2021-10-03
  • 2021-11-08
  • 2021-09-30
  • 2021-10-19
猜你喜欢
  • 2021-11-03
  • 2021-11-30
  • 2021-12-09
  • 2021-09-17
  • 2021-11-06
  • 2021-08-07
  • 2021-11-14
相关资源
相似解决方案