Java中List向前和向后遍历
Posted 走在修行的大街上
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中List向前和向后遍历相关的知识,希望对你有一定的参考价值。
Java中List向前和向后遍历
import java.util.*;
public class TestCollectionIterator {
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
al.add("Amit");
al.add("Vijay");
al.add("Kumar");
al.add(1,"Sachin");
System.out.println("element at 2nd position: "+al.get(2));
ListIterator<String> itr=al.listIterator();
System.out.println("traversing elements in forward direction...");
while(itr.hasNext()){
System.out.println(itr.next());
}
System.out.println("
traversing elements in backward direction...");
while(itr.hasPrevious()){
System.out.println(itr.previous());
}
}
}
element at 2nd position: Vijay
traversing elements in forward direction...
Amit
Sachin
Vijay
Kumar
traversing elements in backward direction...
Kumar
Vijay
Sachin
Amit
以上是关于Java中List向前和向后遍历的主要内容,如果未能解决你的问题,请参考以下文章
每次在libgdx中单击按钮时如何向前和向后移动Sprite?