This program demonstrates how to replace particular elements in an ArrayList in Java. The ArrayList contains a list of fruits and the program replaces the elements at index 2 and 5 with "Mulberry" and "Guava" respectively. Here's a breakdown of the code:
import java.util.ArrayList; public class ReplaceElement_Particular { public static void main(String[] args) { ArrayList<String> fru_list = new ArrayList<String>(); fru_list.add("Pineapple"); fru_list.add("Papaya"); fru_list.add("Mango"); fru_list.add("Apple"); fru_list.add("Banana"); fru_list.add("Cherry"); fru_list.add("Watermelon"); System.out.println("Given ArrayList : "+fru_list); //Replacing the element at index 2,5 with "Mulberry","Guava" fru_list.set(2, "Mulberry"); fru_list.set(5, "Guava"); System.out.println("After Replace Element in ArrayList : "+fru_list); } }
Given ArrayList : [Pineapple, Papaya, Mango, Apple, Banana, Cherry, Watermelon] After Replace Element in ArrayList : [Pineapple, Papaya, Mulberry, Apple, Banana, Guava, Watermelon]
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions