实现RandomAccess接口的集合比如ArrayList,应当使用最普通的for循环而不是foreach循环来遍历

 

        if(list instanceof RandomAccess){
            int length = list.size();
            for (int i = 0; i < length; i++) {
                list.get(i);
            }
        }else{
            Iterator<?> it = list.iterator();
            while(it.hasNext()){
                it.next();
            }
            
        }

 

相关文章:

  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2021-05-01
  • 2021-11-01
  • 2021-12-26
猜你喜欢
  • 2022-12-23
  • 2021-12-10
  • 2021-07-23
  • 2021-11-19
  • 2021-04-29
  • 2021-06-21
相关资源
相似解决方案