转自:
下文讲述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) { }