Java Collection Tutorial - Java LinkedList.set(int index, E element)








Syntax

LinkedList.set(int index, E element) has the following syntax.

public E set(int index, E element)

Example

In the following code shows how to use LinkedList.set(int index, E element) method.

//from  w  ww.j  a  v  a 2  s.c  o m
import java.util.LinkedList;

public class Main {

   public static void main(String[] args) {

      // create a LinkedList
      LinkedList<String> list = new LinkedList<String>();

      // add some elements
      list.add("Hello");
      list.add("from java2s.com");

      // print the list
      System.out.println("LinkedList:" + list);

      // set "Coffee" at index 1
      System.out.println("Object to be replaced:" + list.set(1, "java2s.com"));

      // print the list
      System.out.println("LinkedList:" + list);

   }
}

The code above generates the following result.