码迷,mamicode.com
首页 > 编程语言 > 详细

Java中List向前和向后遍历

时间:2018-11-11 15:00:01      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:array   \n   stc   system   add   while   new   ack   int   

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向前和向后遍历

标签:array   \n   stc   system   add   while   new   ack   int   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!