Remove element from specified Index in Java Araylist

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

/*
 * Here we will learn to remove an Element from specificed Index
 */
public class RemoveElementFromIndex {
	public static void main(String args[]) {
		List<Integer> list = new ArrayList<Integer>();

		/*
		 * add() method appends the specified element to the end of given list.
		 */
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);

		System.out.println("Elements in List");
		for (int i : list)
			System.out.println(i);

		System.out.println("Removing element from list");
		/*
		 * remove(int index) method removes the element at the specified
		 * position in list. Shifts any subsequent elements to the left
		 * (subtracts one from their indices).
		 * 
		 * Note*: This method returns the element that was removed from the
		 * list.
		 */
		int elementRemoved = list.remove(2);
		System.out.println("Removed Element from the list is: "
				+ elementRemoved);

		System.out.println("Elements in List after removing above element");
		for (int i : list)
			System.out.println(i);

	}

}

 

By | 2012-09-18T13:43:33+00:00 September 18th, 2012|Code Base|0 Comments

About the Author:

Leave A Comment