Removing an element from Array on Runtime in Java

java remove element from array and shift
remove element from string array java
how to remove element from array in java without using collections
remove element from arraylist java
remove duplicate element from array? - java
remove alternate element from array in java
how to remove an element from an array in java
java remove element from array in loop

Is there a way to remove an element from a an Array on Runtime?

For Example:

int[] num =  {8, 1, 4, 0, 5};

Output:
Enter the Index: 0
1, 4, 0, 5
Enter the Index: 3
1, 4, 0
Enter the Index: 1
4, 0;

I know that you cannot resize an Array's length once it is initialized and in this kind of sample problem, using an ArrayList is much more practical. However, is there a way to do this kind of problem by using just an array?

I've managed to remove one element and display the array -1 by creating new array and copying the original array's values in it. But the problem is, in the next iteration in the Output I can still remove an element but the size does not change.

This is what happens:

int[] num =  {8, 1, 4, 0, 5};

Output:
Enter the Index: 0
1, 4, 0, 5  // in the first loop it goes as I want it.
Enter the Index: 2
1, 4, 5, 5  // this time array's length is still 4 and just duplicates the last value
Enter the Index: 1
1, 5, 5, 5  // length is still the same and so on.

This is my code in removing an element from an array:

public static int[] removeElement(int index, int[] n) {

    int end = n.length;

    for(int j = index; j < end - 1; j++) {
        n[j] = n[j + 1];            
    }
    end--;

    int[] newArr = new int[end];
    for(int k = 0; k < newArr.length; k++) {
        newArr[k] = n[k];
    }

    displayArray(newArr);        

    return newArr;
}

public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     int[] num = {8, 1, 4, 0, 5};

     for(int i = 0; i < num.length; i++) {
          System.out.print("Enter the Index: ");
          int index = input.nextInt();
          removeElement(index, num);
     }
}

public static void displayArray(int[] n) {
     int i = 0;
     for(; i < n.length - 1; i++) {
          System.out.print(n[i] + ", ");
     }
     System.out.print(n[i]);
}

Is there a trick on how to do this on Arrays? Or do I really have to use ArrayList?

You are discarding the new array returned by removeElement.

Change your loop to:

for(int i = 0; i < num.length; i++) {
     System.out.print("Enter the Index: ");
     int index = input.nextInt();
     num = removeElement(index, num);
}

How to Remove Duplicates from Array Without Using , How do you remove an element from an array in Java? Java program to remove an element from an array, deleting element from an array in Java. If you have to write your own Java program to remove an element from an array then you will have to shift all the elements, to the left, that come after the element that has to be removed.

From your code, you are not actually removing an element from an array. You are in fact creating a new array, with size 1 less than previous array and filling the new array with the old array's remaining values.

In addition, your logic to remove an element from the old array is wrong. To start, your old array still has the same size, all you are doing is replace the array's element at index position with the element at index+1 position.

How to Remove an Element from Array in Java with Example, How do you remove an element from an array in Java without collections? Remove/Delete An Element From An Array In Java. In this tutorial, we will discuss the various methods to delete an element from an array. It includes: Using another Array; Using Java 8 streams; Using ArrayList; Using System.arraycopy() Using Another Array. This is the traditional and somewhat inefficient method of deleting an array element.

You can try this code:

public static int[] removeElement(int index, int[] arr) {
    int length = arr.length - 1;
    int[] res = new int[length];
    for(int i = 0; i < index; i++) {
        res[i] = arr[i];
    }
    for(int i = index; i < length; i++) {
        res[i] = arr[i + 1];
    }
    return res;
}

The idea of code snippet above is to copy the array to a new one (with length less by one) skipping the element we'd like to remove

How to remove an element from ArrayList in Java?, How do you remove all elements from an array in Java? There is no direct way to remove elements from an Array in Java. Though Array in Java objects, it doesn't provide any methods to add(), remove() or search an element in Array. This is the reason Collection classes like ArrayList and HashSet are very popular. Thanks to Apache Commons Utils, You can use there ArrayUtils class to remove an element

Removing an Element, There is no direct way to remove elements from an Array in Java. Though Array in Java objects, it doesn't provide any methods to add(), remove() or search an element in Array. This is the reason Collection classes like ArrayList and HashSet are very popular. If you need to be able to easily remove an element from an ordered group, then it's pretty clear that perhaps an array is the wrong kind of group to use in the first place. So List is a good suggestion, and Set might be better, depending on the app. – Ben Hardy Mar 13 '09 at 23:41

Remove element from an Array Java, remove(Obejct obj) : Accept object to be removed. What happens when we have an integer arrayList and we want to remove an item? For example consider below  Using Java 8 Streams: . Approach: Get the array and the index. Convert the array into IntStream using IntStream.range() method. Remove the specified index element using filter() method.

Remove an element at specific index from array in Java, To remove an element from a list use the remove() method. This method removes an element without leaving a hole. The other object references in the list are moved to fill the gap. E remove(int index) // Delete the element at index . If the ArrayList does not contain duplicates, we can simply pass the last element value to be deleted to the remove() method, and it will delete that value. Note : Incase the ArrayList contains duplicates, it will delete the first occurrence of the object passed as a parameter to the remove() method.

Comments
  • I see your array only contains positive numbers. Maybe you can replace the last number by -1, so instead when displaying the array, you run over the array until you arrive at value -1.
  • @Eran done editting
  • @robert, it's a suggestion.. Its better to go with Java Collections...