Iterate through elements ArrayList using ListIterator : ListIterator « Collections « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Collections » ListIterator 




import java.util.ArrayList;
import java.util.ListIterator;

public class Main {

  public static void main(String[] args) {

    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");

    ListIterator itr = arrayList.listIterator();
    System.out.println("in forward direction");
    while (itr.hasNext()) {
      System.out.println(itr.next());
    }

    System.out.println("in backward direction");
    while (itr.hasPrevious()) {
      System.out.println(itr.previous());
    }
  }
}
/*in forward direction
1
2
3
4
5
in backward direction
5
4
3
2
1
*/














9.38.ListIterator
9.38.1.ListIterator: Like an Iterator
9.38.2.Iterate through elements ArrayList using ListIterator
9.38.3.ListIterator: In reverse order
9.38.4.Get Previous and next index using Java ListIterator
9.38.5.Add or insert an element to ArrayList using Java ListIterator
9.38.6.Remove an element from ArrayList using Java ListIterator
9.38.7.Replace an element from ArrayList using Java ListIterator
9.38.8.Modifying the list that the iterator came from: add(), remove(), and set()
9.38.9.Iterate through elements of Java LinkedList using ListIterator
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.