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.
How can i remove an element from array in Java - Stack Overflow
java - How to remove unused elements from an array without the use of the Collections - Stack Overflow
[java] How do I remove an element from an array?
[Java] Inserting and deleting elements in an array
There are no "empty" elements in an array, and you can't resize arrays either. All you can do is to put all of the elements you're interested in at one end of the array (e.g. starting at zero) with no "uninteresting" elements between them, and then construct a string from a portion of the array.
Use another variable to store the index of the next "empty" element in the array: every time you find a letter, increment this variable. Once you've finished iterating letters, this variable will contain the length of the array to use to create the string.
char[] copyLoop = new char[26];
int dst = 0;
for (int i = 0; i < letters.length; i++) {
if (letters[i] > 1) {
char c = (char) (i + 97);
copyLoop[dst++] = c;
}
}
return new String(copyLoop, 0, dst);
Or, of course, use a StringBuilder (which is doing effectively the same internally):
StringBuilder copyLoop = new StringBuilder(26);
for (int i = 0; i < letters.length; i++) {
if (letters[i] > 1) {
char c = (char) (i + 97);
copyLoop.append(c);
}
}
return copyLoop.toString();
Unfortunately not. Arrays in Java are not shrinkable/expandable, their size is set constant. There are two ways you can work around:
Use Collections, i.e. create a List of Characters
ArrayList<Character> list = new ArrayList<>();, use it and delete items you don't need after each loop cycle.On every cycle of your for loop, when you need to delete an item inside your Array, just create a new char[] array, which is smaller and copy the contents of the old one to the new, except for the value you deleted.
Also, if you are working with Strings, it's recommended to use mutable String classes s.a. StringBuilder or StringBuffer which let you add/remove characters from the String. In your case that seems to be the best solution.
For example,
public class elementRemover{
public static void main(String[] args){
int[] array = {2, 2, 3};
for (int i = 0; i < array.length; i++){
for(int j = i + 1; j < array.length; j++){
if (array[i] == array[j]){
array[j] = Integer.MIN_VALUE;
}
}
System.out.println(array[i]);
}
}
}So now my output becomes
2
-2147483648
3
But how do I make it so Integer.MIN_VALUE arrays are print out blanks instead of a number, so it outputs this instead,
2
3