Update Specific Elements Of ArrayList | Java Programs

Output:

*** Update Specific Element ***

ArrayList At First:       [Rahman, Arshad, Mustafa, Haider, Ameen]

Update "Mustafa" with "Arshad" and "Ameen" with "Aiman"

ArrayList After Updation: [Rahman, Arshad, Zubair, Haider, Aiman]

Click Here For Java Online Compiler

Solution:

/**
 *
 * @author Alee Ahmed Kolachi
 */
import java.util.ArrayList;

public class UpdateElementInArrayList {

    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        System.out.println("*** Update Specific Element ***" + "\n");
        arrayList.add("Rahman");
        arrayList.add("Arshad");
        arrayList.add("Mustafa");
        arrayList.add("Haider");
        arrayList.add("Ameen");
        System.out.print("ArrayList At First:       ");
        System.out.println(arrayList);
        System.out.println("\nUpdate \"Mustafa\" with \"Arshad\" and \"Ameen\" with \"Aiman\"");
        // Update the third element with "Zubair"
        // and fifth element with "Aiman"
        arrayList.set(2, "Zubair");
        arrayList.set(4, "Aiman");
        // Print the list again
        System.out.println();
        System.out.print("ArrayList After Updation: ");
        System.out.println(arrayList);
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon