Sunday, 10 May 2015

How to replace element in ArrayList


 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: 

 


Storing Java Object In ArrayList





No comments:

Post a Comment