Export (0) Print
Expand All

List.iterator Method

Visual Studio 2005

Provides an iterator to iterate over a List object.

Package: java.util

Assembly: vjslib (in vjslib.dll)


public abstract java.util.Iterator iterator();

An iterator that can be used over the List object.

In this example, you create an initialize a collection and then you retrieve an iterator to iterate over the collection and display its members.

// list-iterator1.jsl
// List.iterator example

import java.util.*;

public class MyClass
{
   public static void main()
   {
      Collection hSet = new HashSet();

      // Add some elements to the HashSet:
      hSet.add("1 ");
      hSet.add("2 ");
      hSet.add("3 ");
      hSet.add("4 ");

      // Retrieve an iterator to the hashset:
      Iterator iter = hSet.iterator();
 
      // Extract elements from iterator.
      // Note that the elements may not follow the order in which they
      // are added to HashSet.
      while(iter.hasNext()) 
      {
         System.out.print(iter.next());
         iter.remove();
      }
   }
}
/*
Output:
3 1 4 2
*/

Community Additions

ADD
Show:
© 2014 Microsoft