Skip to Content

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