Q:

Java program to replace element within the ArrayList

belongs to collection: Java ArrayList Programs

0

Given an ArrayList and we have to replace element of it using Java program.

Example:

Input:
55
25
368

Element to replace ‘25’ with  ‘6’
Output:
55
6
368

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program

import java.util.ArrayList;

public class ExArrayReplace {
  public static void main(String[] args) {
    // Create an object of arrayList. 
    ArrayList arrayList = new ArrayList();

    //Add elements to arraylist.

    arrayList.add("55");
    arrayList.add("25");
    arrayList.add("368");

    System.out.println("Original ArrayList...");

    for (int index = 0; index < arrayList.size(); index++)
      System.out.println(arrayList.get(index));

    // Enter the position and number for replace.
    arrayList.set(1, "6");

    // Print arraylist after replacement.
    System.out.println("ArrayList after replacement...");
    for (int index = 0; index < arrayList.size(); index++)

      System.out.println(arrayList.get(index));
  }
}

Output

Original ArrayList...
55
25
368
ArrayList after replacement...
55
6
368

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How to extract some of the elements from given lis... >>
<< Java program to find index of an element from an A...