You could use commons lang's ArrayUtils.
array = ArrayUtils.removeElement(array, element)
commons.apache.org library:Javadocs
Answer from Peter Lawrey on Stack OverflowHow can i remove an element from array in Java - Stack Overflow
Remove specific index from array in java - Stack Overflow
Having trouble with java arrays, removing an element then returning a new size of an array
java - How to remove an item from an array at index? - Stack Overflow
Videos
You could use commons lang's ArrayUtils.
array = ArrayUtils.removeElement(array, element)
commons.apache.org library:Javadocs
Your question isn't very clear. From your own answer, I can tell better what you are trying to do:
public static String[] removeElements(String[] input, String deleteMe) {
List result = new LinkedList();
for(String item : input)
if(!deleteMe.equals(item))
result.add(item);
return result.toArray(input);
}
NB: This is untested. Error checking is left as an exercise to the reader (I'd throw IllegalArgumentException if either input or deleteMe is null; an empty list on null list input doesn't make sense. Removing null Strings from the array might make sense, but I'll leave that as an exercise too; currently, it will throw an NPE when it tries to call equals on deleteMe if deleteMe is null.)
Choices I made here:
I used a LinkedList. Iteration should be just as fast, and you avoid any resizes, or allocating too big of a list if you end up deleting lots of elements. You could use an ArrayList, and set the initial size to the length of input. It likely wouldn't make much of a difference.
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().
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];
So guys i'm having trouble finding a solution for this question. I'm kinda frustrated that i didn't solve this kind of problem during my exam for employment.
Problem: The task is to provide an implementation for the given function. int[] removeElem(int[] array, value), the objective is to find if the array contains the given 'value' inside it. if found then remove the element from the array then return an array with a new size, else return original array.
So far this is what i got:
public static void main(String[] args) {
int[] myArray = {5,6,9,4};
int[] newArray = removeElem(myArray,9);
for(int i=0;i<newArray.length;i++){
System.out.print(newArray[i] + " ");
}
}
public static int[] removeElem(int[] array, int value){
int[] tempArray = array;
int newArrayLength = 0;
int[] newArray;
for(int index=0;index < tempArray.length; index++){
if(tempArray[index] == value){
tempArray[index] = -1;
newArrayLength++;
}
}
newArray = new int[tempArray.length - newArrayLength];
for(int index=0;index < newArray.length; index++){
if(tempArray[index] != -1){
newArray[index] = tempArray[index];
}
}
return newArray;
}Unfortunately i can't get it right :/
Edit: Thank you! everyone for suggesting different solutions to this problem, I feel like I need to work more on my solutions and such :|.
you can't delete the index or element of array because it's already sized in the memory so you can't use delete but you can put two values { null or 0 } or another values you want to show that you already get of this.
Arrays cannot be resized. Use a List<Integer> (such as an ArrayList<Integer>) if you want a collection with resizability.
Otherwise, you'll have to implement this yourself by making a copy of the array that is one element smaller than the original array. Then copy all of the values except the one at the specified index from the old array to the new array.