Remove specific index from array in java

Is it possible to remove a specific element from an array by specifying an index value? For example, can you remove a character d

by specifying the index

value 1?

char[] words = { 'c', 'd', 'f', 'h', 'j' };

      

+3


source to share


5 answers


If you don't want to use ArrayList, arraycopy is an alternative:

    System.arraycopy(words, 0, result, 0, i);
    System.arraycopy(words, i+1, result, i, result.length-i);

      

where i is your index to delete.



Hope I can help.

EDIT . Of course, you must first determine the correct array lengths:

char[] result = new char[words.length-1];

      

+1


source


Assuming you don't want your array to contain null values, you will need to make a method that does this. Something like this should be sufficient:

public char[] remove(int index, char[] arr) {
    char[] newArr = new char[arr.length - 1];
    if(index < 0 || index > arr.length) {
        return arr;
    }
    int j = 0;
    for(int i = 0; i < arr.length; i++) {
        if(i == index) {
            i++;
        }
        newArr[j++] = arr[i];
    }

    return newArr;
}

      



Then just replace the old array with the result of remove ().

+1


source


If you need to remove one or more elements from an array without converting it to List

or creating an additional array, you can do it in O (n), regardless of the number of elements to remove.

Here a

is the initial array, int... r

these are the different ordered indices (positions) of the elements to remove:

public int removeItems(Object[] a, int... r) {
    int shift = 0;                             
    for (int i = 0; i < a.length; i++) {       
        if (shift < r.length && i == r[shift])  // i-th item needs to be removed
            shift++;                            // increment `shift`
        else 
            a[i - shift] = a[i];                // move i-th item `shift` positions left
    }
    for (int i = a.length - shift; i < a.length; i++)
        a[i] = null;                            // replace remaining items by nulls

    return a.length - shift;                    // return new "length"
}  

      

Small testing:

Character[] words = {'c','d','f','h','j'};
removeItems(words, 1);
System.out.println(Arrays.asList(words));       // [c, f, h, j, null]

      

+1


source


You cannot remove an element from an array and reduce the size of the array. When you've created an array, the length will be fixed.

You can change the value to something that does not make sense or is considered "empty", but you cannot remove it.
Another option is to use a list like ArrayList. It has a "remove" method that allows you to actually remove an element from it.

0


source


 import java.util.Arrays;
  public class Main
 {
 public static void main(String[] args) {
 int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};

 int removeIndex = 1;
 int j=0;
  for(int i = 0; i < my_array.length -1; i++)
  {

    if(i==1)
    {

    }
    else
     {
        my_array[j] = my_array[i];
        j++;
     }

    }
     System.out.println("Original Array : "+Arrays.toString(my_array));   
    }
  }

      

0


source







All Articles