How to Add Element at the specified position in ArrayList and Remove Element From Specified Index Of ArrayList?

Element addition and Element removal from specified position in ArrayList.


								
package com.mindclues;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListExample {

    public static void main(String[] args) {

        List list = new ArrayList();

        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");

        //Here we are adding element at 3 Index position
        list.add(2, "One");

        Iterator iterator = list.iterator();

        String str = null;

        while (iterator.hasNext()) {

            str = (String) iterator.next();

            System.out.println("List Elements :" + str);

        }

        System.out.println("Size of List before remove element:"+list.size());
        
        String removedElement = (String) list.remove(1);

        System.out.println(removedElement + " is removed from ArrayList");
        
          System.out.println("Size of List after remove :"+list.size());

    }
}
							

								
List Elements :A
List Elements :B
List Elements :One
List Elements :C
List Elements :D
List Elements :E
Size of List before remove element:6
B is removed from ArrayList
Size of List after remove :5
							

Related Articles

post a comment