首页 > 编程语言 > 详细

Java中List向前和向后遍历

时间:2018-11-11 14:56:03      阅读:685      评论:0      收藏:0      [点我收藏+]

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

Java中List向前和向后遍历

原文:https://www.cnblogs.com/hglibin/p/9942013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!