We can replace any specific index element by other element using set(int index,element) method.
import java.util.ArrayList;
public class ReplaceElement {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
// add element in arraylist
arr.add("c");
arr.add("php");
arr.add("html");
arr.add("java");
arr.add("delhi");
arr.add("india");
// print arraylist
System.out.println("before replace arrayList is = " + arr);
/*
* Replaces the element at the specified
* position in this list with the specified element.
*/
arr.set(2,"Mysql");
System.out.println("after replace arrayList is = " + arr);
}
}
Output:
before replace arrayList is = [c, php, html, java, delhi, india]
after replace arrayList is = [c, php, Mysql, java, delhi, india]
Related Posts:
How to count number of element in ArrayList
How to insert element in arraylist
How to use retailAll in arraylist
No comments:
Post a Comment