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("\ntraversing 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

</div>

相关文章:

  • 2022-02-07
  • 2022-02-19
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-26
  • 2021-11-15
  • 2022-12-23
  • 2021-09-10
  • 2021-12-06
  • 2021-06-15
相关资源
相似解决方案