You could use commons lang's ArrayUtils.
array = ArrayUtils.removeElement(array, element)
commons.apache.org library:Javadocs
Answer from Peter Lawrey on Stack OverflowYou 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.
Videos
Having trouble with java arrays, removing an element then returning a new size of an array
How can i remove an element from array in Java - Stack Overflow
Looking for a optimal way to delete element from object[] java
Removing an item from an array list
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 :|.
- Create a new array of size
n-1. - Copy
0..index-1elements from the old array to the new array. - Copy
index+1..nelements from the old array to the new array.
Use java.lang.System.arraycopy(Object, int, Object, int, int) for array copy operations.
This would be probably the most optimal way.
Another approach using streams could look like
Object[] myObject = {"foo", "bar", 42, 13, "doo"};
int indexToRemove = 3;
Object[] result = IntStream.range(0, myObject.length)
.filter(i -> i != indexToRemove)
.mapToObj(i -> myObject[i])
.toArray();