Iterate through elements Java ArrayList using ListIterator Example

  1. /*
  2.   Iterate through elements Java ArrayList using ListIterator Example
  3.   This Java Example shows how to iterate through the elements of java
  4.   ArrayList object in forward and backward direction using ListIterator.
  5. */
  6.  
  7. import java.util.ArrayList;
  8. import java.util.ListIterator;
  9.  
  10. public class IterateThroughArrayListUsingListIteratorExample {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. //create an ArrayList object
  15. ArrayList arrayList = new ArrayList();
  16.  
  17. //Add elements to Arraylist
  18. arrayList.add("1");
  19. arrayList.add("2");
  20. arrayList.add("3");
  21. arrayList.add("4");
  22. arrayList.add("5");
  23.  
  24. /*
  25.   Get a ListIterator object for ArrayList using
  26.   listIterator() method.
  27.   */
  28. ListIterator itr = arrayList.listIterator();
  29.  
  30. /*
  31.   Use hasNext() and next() methods of ListIterator to iterate through
  32.   the elements in forward direction.
  33.   */
  34. System.out.println("Iterating through ArrayList elements in forward
  35. direction...");
  36. while(itr.hasNext())
  37. System.out.println(itr.next());
  38.  
  39. /*
  40.   Use hasPrevious() and previous() methods of ListIterator to iterate through
  41.   the elements in backward direction.
  42.   */
  43. System.out.println("Iterating through ArrayList elements in backward
  44. direction...");
  45. while(itr.hasPrevious())
  46. System.out.println(itr.previous());
  47.  
  48. }
  49. }
  50.  
  51. /*
  52. Output would be
  53. Iterating through ArrayList elements...
  54. Iterating through ArrayList elements in forward direction...
  55. 1
  56. 2
  57. 3
  58. 4
  59. 5
  60. Iterating through ArrayList elements in backward direction...
  61. 5
  62. 4
  63. 3
  64. 2
  65. 1
  66. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

nice

thx 4 helping me learning java.. anyway.. i don't know how to start to learn java.. can you tell me the steps of learning.. coz 2 learn all about java almost impossible to me.. so please guid me.. the steps and the important part of java.. tq


Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!