Remove specific index from array in java
Solution 1
Assuming you do not want your array to contain null values, then you would have to make a method that does it. Something like this should suffice:
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().
Solution 2
If you need to remove one or multiple elements from array without converting it to List
nor creating additional array, you may do it in O(n) not dependent on count of items to remove.
Here, a
is initial array, int... r
are distinct ordered indices (positions) of 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]
Solution 3
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 should initially define the correct array lengths:
char[] result = new char[words.length-1];
GES Vadamanappakkam
Updated on June 04, 2022Comments
-
GES Vadamanappakkam almost 2 years
Can I remove a specific element from array by mentioning index value? For example can I remove the character
d
by givingindex
value 1?char[] words = { 'c', 'd', 'f', 'h', 'j' };
-
Sotirios Delimanolis over 9 yearsWhat do you mean by remove?
-
GES Vadamanappakkam over 9 yearsdelete the character
-
Sotirios Delimanolis over 9 yearsAgain, what does delete the character mean?
-
brso05 over 9 yearsyou can't remove per-say but you can set that index to some value that is not used or you could create a new array with all the values except the one you want to remove.
-
GES Vadamanappakkam over 9 yearsif i give index value 1 i need get array like c,f,h,j. if i give index value 0 i need to get array like d,f,h,j. now clear
-
Sotirios Delimanolis over 9 yearsHere's the official tutorial on arrays. The first sentence has your answer.
-
Mark W over 9 yearsUse an arraylist and its .remove() method. Arrays are immutable, so you cant 'remove' one, but rather create a new array of length -1 then populate it from the source.
-
GES Vadamanappakkam over 9 yearsyes thank u @brso05...
-
brso05 over 9 years@GESVadamanappakkam your welcome glad I could help!
-
-
lenhuy2106 over 9 yearsbtw, i/you should name it char[] word or char[] letters. also, your definition is wrong, it should be:
char[] word = {'c','d','f','h','j'};
-
Eugene Kartoyev over 5 yearsIt throws an ArrayIndexOutOfBoundsException if the index is the last element. So, instead of i++, one should use continue.