How to Replace the value of existing element in ArrayList jede bekanntschaft jede sympathische begegnung ist ein gewinn enter site sims dating online games free rencontre celibataire 85 best dating sites to join partnersuche geretsried follow link How to Replace the value of existing element in ArrayList

If there is a need to update the list element based on the index then set method of ArrayList class can be used. The method set(int index, Element E) updates the element of specified index with the given element E.

Syntax: public E set(int index, Element E)


In this example I have an ArrayList of Integer Type where I have added few elements and then I’m updating few of elements using set method of java.util.ArrayList class.

package ArraylistClass;

import java.util.ArrayList;

public class SetExample {

  public static void main(String[] args) {
    ArrayList<Integer> arraylist = new ArrayList<Integer>();
      System.out.println("ArrayList before update: "+arraylist);
      //Updating 1st element
      arraylist.set(0, 11);
      //Updating 2nd element
      arraylist.set(1, 22);
      //Updating 3rd element
      arraylist.set(2, 33);
      System.out.println("ArrayList after Update: "+arraylist);




ArrayList before update: [1, 2, 3, 4, 5]
ArrayList after Update: [11, 22, 33, 4, 5]