How to replace an element at specified index of Java ArrayList?

To replace an element at the specified index of ArrayList use Object set(int index,object obj) method. This method replaces the specified element at the specified index in the ArrayList and returns the element previously at the specified position.


								
package com.tech.mindclues;

import java.util.ArrayList;

public class ReplaceElementInAnArrayList {

	public static void main(String arg[]) {

		ArrayList listobj = new ArrayList();

		listobj.add("Mind");
		listobj.add("Clues");
		listobj.add("Hunt");
		listobj.add("Hagrid");
		listobj.add("Bond");
		listobj.add("Tom");

		listobj.set(1, "Mindclues");
	
		for(String str : listobj){
			
			System.out.println(str);
			
		}
	}

}

							

								
Mind
Mindclues
Hunt
Hagrid
Bond
Tom

							

Related Articles

post a comment