Remove Array Elements at Specific Index in Java?

There are following way to Remove Array Elements at Specific Index in Java.

  • Create removeElement() method with arr[] and index input value.
  • Size of input arr[] find from [arr.length-1] method with reference a[].
  • Iterate the for loop from int i=0 to arr.length-1.
  • Check if I index is equals to given index then iteration will be continue.
  • if index is not equals to arr[] index input then store the remaining data from a[k++] = arr[i];
  • Print the array value.
import java.util.Arrays;

public class Main {
	public static int[] removeElement(int arr[], int index) {
		int k=0;
		int arrNew[] = new int[arr.length - 1];
		for (int i = 0;i < arr.length; i++) {
			if (i != index) {
				arrNew[k++] = arr[i];
			}
		}
		return arrNew;
	}

	public static void main(String[] args) {
		int arr[] = { 2,4,6,8,10,12 };
		System.out.println("before deleting array value :- " + Arrays.toString(arr));
		int index = 4;
		arr = removeElement(arr, index);
		System.out.println("before deleting array value :- " + Arrays.toString(arr));
	}
}

Output :-
before deleting array value :- [2, 4, 6, 8, 10, 12]
before deleting array value :- [2, 4, 6, 8, 12]