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








Syntax

ArrayList.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 ArrayList.set(int index, E element) method.

import java.util.ArrayList;
//from   w  w  w .  j  av a 2s.c  om
public class Main {
   public static void main(String[] args) {
      
      ArrayList<Integer>  arrlist = new ArrayList<Integer> (5);

      arrlist.add(1);
      arrlist.add(2);
      arrlist.add(2);
      arrlist.add(2);

      System.out.println(arrlist);
 
      // change element to 55 at 3rd position
      arrlist.set(2,55);
    
      System.out.println(arrlist);
   }
}

The code above generates the following result.