Skip to Content

Traverse through ArrayList in reverse direction using Java ListIterator Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.   Traverse through ArrayList in reverse direction using Java ListIterator Example
  3.   This Java Example shows how to iterate through an ArrayList object in reverse
  4.   direction using Java ListIterator's previous and hasPrevious methods .
  5. */
  6.  
  7. import java.util.ListIterator;
  8. import java.util.ArrayList;
  9.  
  10. public class TraverseReverseUsingListIteratorExample {
  11.  
  12.   public static void main(String[] args) {
  13.     //create an object of ArrayList
  14.     ArrayList aList = new ArrayList();
  15.    
  16.     //Add elements to ArrayList object
  17.     aList.add("1");
  18.     aList.add("2");
  19.     aList.add("3");
  20.     aList.add("4");
  21.     aList.add("5");
  22.    
  23.     //Get an object of ListIterator using listIterator() method
  24.     ListIterator listIterator = aList.listIterator();
  25.  
  26.     //Traverse in forward direction  
  27.     System.out.println("Traversing ArrayList in forward direction
  28.                                             using ListIterator");
  29.     while(listIterator.hasNext())
  30.       System.out.println(listIterator.next());
  31.      
  32.     /*
  33.       Traverse the ArrayList in reverse direction using hasPrevious and previous
  34.       methods of ListIterator. hasPrevious method returns true if
  35.       ListIterator has more elements to traverse in reverse direction.
  36.       Previous method returns previous element in the list.
  37.     */
  38.     System.out.println("Traversing ArrayList in reverse direction
  39.                                                         using ListIterator");
  40.     while(listIterator.hasPrevious())
  41.       System.out.println(listIterator.previous());
  42.      
  43.   }
  44. }
  45.  
  46. /*
  47. Traversing ArrayList in forward direction using ListIterator
  48. 1
  49. 2
  50. 3
  51. 4
  52. 5
  53. Traversing ArrayList in reverse direction using ListIterator
  54. 5
  55. 4
  56. 3
  57. 2
  58. 1
  59. */