Export (0) Print
Expand All

ListIterator.set Method

Visual Studio 2005

Sets an element in a ListIterator collection.

Package: java.util

Assembly: vjslib (in vjslib.dll)


public abstract void set(
    java.lang.Object e);

Parameters

e

The object to be set.

In this example, you construct a collection and create a ListIterator before position 0. A call to next, at this point, will return the first element in the collection. Then you change the first element by using set, and display the new collection.

// LIter-set1.jsl
// ListIterator.set example

import java.util.*;

public class MyClass
{
    public static void main(String[] args)
    {
        // Create a linked list object:
        LinkedList lList = new LinkedList();

        // Add some elements:
        lList.add("Isabella");
        lList.add("Angelina");
        lList.add("Bianca");

        // Get a list iterator before the starting index:
        ListIterator li = lList.listIterator();

        // Display the list:
        System.out.println("The collection: " + lList);

        // Dispaly the next element:
        System.out.println("The next element: " + li.next());
      
         // Change the last called by next():
        li.set("Sally");

        // Display the new list:
        System.out.println("The new collection: " + lList);
    }
}

/*
Output:
The collection: [Isabella, Angelina, Bianca]
The next element: Isabella
The new collection: [Sally, Angelina, Bianca]
*/

Throws IllegalStateException if no call to next or previous has been made.

Community Additions

ADD
Show:
© 2015 Microsoft