This documentation is archived and is not being maintained.

List.set Method

Visual Studio 2005

Inserts an element at the specified location.

Package: java.util

Assembly: vjslib (in vjslib.dll)


public abstract java.lang.Object set(
    int ix,
    java.lang.Object e);

Parameters

ix

The index of the element.

e

The List element to be added.

The new element in the List object.

// list-set1.jsl
// List.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("My node");
        lList.add("Her node");
        lList.add("Their node");
        lList.add("Your node");

        // Display the elements:
        System.out.println("The old list:");
        for (int i=0; i<lList.size(); i++)
            System.out.println(i + "=" + lList.get(i));

        // Set the element #2:
           System.out.println("\nThe following element has been set: "
                              + lList.set(2,"His node"));

        // Display the elements after changing #2:
        System.out.println("\nThe new list:");
        for (int i=0; i<lList.size(); i++)
            System.out.println(i + "=" + lList.get(i));
    }
}

/*
Output:
The old list:
0=My node
1=Her node
2=Their node
3=Your node

The following element has been set: Their node

The new list:
0=My node
1=Her node
2=His node
3=Your node
*/

Throws IndexOutOfBoundsException if ix is negative, or greater than or equal to size.

Show: