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.
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 :|.
In java when an array is initialized, it preserves the memory for that initialization which it needs to hold the data corresponding to the data type of the array declared.
So deleting an array element is something you can not do.
Simple thing that you can do is to shift the other array elements left by overriding the array element that need to be removed from array. Which means declaring new variable that holds the number of elements that the array is currently holding.
Then removing array element becomes simple as this.
shift elements to left starting from the element that you want to be removed.
decrement the variable by 1
When you want to get the current array elements, you can simply loop through the array with reference to the no of elements variable.
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.
Though
Thanks to Apache Commons Utils, You can use there
ArrayUtilsclass to remove an element from array more easily than by doing it yourself. One thing to remember is that Arrays are fixed size in Java, once you create an array you can not change their size, which means removing or deleting an item doesn't reduce the size of the array. This is, in fact, the main difference between Array and ArrayList in Java.
What you need to do is create a new array and copy the remaining content of this array into a new array using System.arrayCopy() or any other means. In fact, all other API and functions you will use do this but then you don't need to reinvent the wheel.
Copy import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;
....
//let's create an array for demonstration purpose
int[] test = new int[] { 101, 102, 103, 104, 105};
System.out.println("Original Array : size : "
+ test.length );
System.out.println("Contents : " + Arrays.toString(test));
// let's remove or delete an element from an Array
// using Apache Commons ArrayUtils
test = ArrayUtils.remove(test, 2); //removing element at index 2
// Size of an array must be 1 less than the original array
// after deleting an element
System.out.println("Size of the array after
removing an element : " + test.length);
System.out.println("Content of Array after
removing an object : " + Arrays.toString(test));