Java中如何对LinkedList进行遍历呢?
下文讲述Linkedlist遍历的方法分享,如下所示:
for循环(随机访问)遍历
int size = list.size();
for (int i=0; i<size; i++) {
list.get(i);
}
for--each循环遍历
for (Integer integ:list){
;
}
迭代器iterator遍历
for(Iterator iter = list.iterator(); iter.hasNext();)
iter.next();
用pollFirst()遍历
while(list.pollFirst() != null)
;
用pollLast()遍历
while(list.pollLast() != null)
;
用removeFirst()遍历
try {
while(list.removeFirst() != null)
;
} catch (NoSuchElementException e) {
}
用removeLast()遍历
try {
while(list.removeLast() != null)
;
} catch (NoSuchElementException e) {
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


