Problem Statement:
How to remove an element from an array ?
Solution Approach:
- Create an Array List with values
- Call ArrayList.remove(index)
- Print the Array List Values
package com.java.planforexams.array;
import java.util.ArrayList;
public class RemoveElementFromArray {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>(3);
arrayList.add(0,11);
arrayList.add(1,21);
arrayList.add(2,31);
System.out.println("Current Array List"+arrayList);
arrayList.remove(0);
System.out.println("Array List after removing 1st Index element");
for (Integer arrayElement : arrayList) {
System.out.println("Element is " +arrayElement);
}
arrayList.remove(1);
System.out.println("Array List after removing 2nd Index element");
for (Integer arrayElement : arrayList) {
System.out.println("Element is " +arrayElement);
}
}
}
Output:
Current Array List[11, 21, 31]
Array List after removing 1st Index element
Element is 21
Element is 31
Array List after removing 2nd Index element
Element is 21