List遍历方式

@ List遍历方式,哪种方式最快

//方法1 集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代

    Iterator it1 = list.iterator();
    while(it1.hasNext()){
        System.out.println(it1.next());
    }


    //方法2 集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代
    for(Iterator it2 = list.iterator();it2.hasNext();){
        System.out.println(it2.next());
    }

    //方法3 增强型for循环遍历
    for(String value:list){
        System.out.println(value);
    }



    //方法4 一般型for循环遍历
    for(int i = 0;i < list.size(); i ++){
        System.out.println(list.get(i));
    }

ArrayList方法四的遍历方式最快,方法三的内部实现也是迭代器,迭代器都要先判断是否有下一个,所以导致执行比直接通过下标取值慢。
LinkList速度差不多。

猜你喜欢

转载自blog.csdn.net/w517272812/article/details/88739995