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








Syntax

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

E set(int index, E element)

Example

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

//from  w  ww  . java  2 s.c  o m
import java.util.Arrays;
import java.util.List;
public class Main {
  public static void main(String[] a) {
    List<String> list = Arrays.asList(new String[] { "A", "B", "C", "D" });
    list.set(2, "X");
    System.out.println(list);
  }
}

The code above generates the following result.