by @kodeazy

Java How to replace elements at specific index in ArrayList?

Home » java » Java How to replace elements at specific index in ArrayList?
  • In this blog we will discuss on how to replcae element at specific index in java ArrayList.
  • we use set(int index,E element) method for replacing at the specific index.
  • Below is the example syntax.

    arrayIntegers.set(2,90);
  • Below is the example program to replace element at specific index i ArrayList.

    import java.util.*;
    class ArrayListReplaceIndex{
    public static void main(String args[]){
    	List<Integer> arrayIntegers = Arrays.asList(10,30,24,60,55);
    	System.out.println("Elements before replacing"+arrayIntegers);
    	arrayIntegers.set(2,90);
    	System.out.print("Elements after replacing element at second index "+arrayIntegers);
    }
    }   

    Output:

    D:\java>javac ArrayListReplaceIndex.java
    D:\java>java ArrayListReplaceIndex
    Elements before replacing [10, 30, 24, 60, 55]
    Elements after replacing element at second index [10, 30, 90, 60, 55]