In Java you can't delete elements from an array. But you can either:
Create a new char[] copying only the elements you want to keep; for this you could use System.arraycopy() or even simplerArrays.copyOfRange(). For example, for copying only the first three characters of an array:
char[] array1 = {'h','m','l','e','l','l'};
char[] array2 = Arrays.copyOfRange(array1, 0, 3);
Or use a List<Character>, which allows you to obtain a sublist with a range of elements:
List<Character> list1 = Arrays.asList('h','m','l','e','l','l');
List<Character> list2 = list1.subList(0, 3);
Answer from Óscar López on Stack OverflowIn Java you can't delete elements from an array. But you can either:
Create a new char[] copying only the elements you want to keep; for this you could use System.arraycopy() or even simplerArrays.copyOfRange(). For example, for copying only the first three characters of an array:
char[] array1 = {'h','m','l','e','l','l'};
char[] array2 = Arrays.copyOfRange(array1, 0, 3);
Or use a List<Character>, which allows you to obtain a sublist with a range of elements:
List<Character> list1 = Arrays.asList('h','m','l','e','l','l');
List<Character> list2 = list1.subList(0, 3);
You can use Arrays.copyOfRange like this:
array1 = Arrays.copyOfRange(array1, 2, 5);
More info
java - Remove duplicate elements from char array - Code Review Stack Exchange
java - How to remove specific element from an array - Stack Overflow
java - Removing a character from an ArrayList of characters - Stack Overflow
Removing an element from an Array (Java) - Stack Overflow
One possibility is to use a Set. Just add the elements from the array and then get the unique elements from the Set. This algorithm will be O(n) instead of O(n^2) because each element is accessed only once.
Note that you can use Arrays.asList() to simplify the coding, too.
Here is a slightly convoluted algorithm that yields the same result as yours but is faster. It takes advantage of Java's native support for arrays instead of using an internal String:
private static char[] getYOURCharArray(char[] array) {
char[] distinctChars = new char[0];
char[] distinctCharsInOriginalOrder = new char[array.length];
for (int i = 0; i < array.length; i++) {
int binarySearchResult = Arrays.binarySearch(distinctChars, array[i]);
if (binarySearchResult < 0) {
char[] updatedDistinctChars = new char[distinctChars.length + 1];
int insertionPointForNewChar = -(binarySearchResult + 1);
System.arraycopy(distinctChars, 0, updatedDistinctChars, 0, insertionPointForNewChar);
updatedDistinctChars[insertionPointForNewChar] = array[i];
System.arraycopy(distinctChars, insertionPointForNewChar, updatedDistinctChars, insertionPointForNewChar + 1, distinctChars.length - insertionPointForNewChar);
distinctChars = updatedDistinctChars;
distinctCharsInOriginalOrder[distinctChars.length - 1] = array[i];
}
}
return Arrays.copyOf(distinctCharsInOriginalOrder, distinctChars.length);
}
Using Arrays.binarySearch(char[], char) to look for a char in a char[] seems to be faster than searching for a char in a String using String.indexOf(int). On the other hand, Arrays.binarySearch(char[], char) requires the char[] to be sorted, which is why we need a second char[] that stores all distinct characters in the order they were first encountered in the original char[], assuming the returned char[] must fulfill this requirement (if it doesn't, then the array distinctCharsInOriginalOrder is actually not needed and you can return distinctChars directly at the end of this method, which might speed up the process a little bit). To ensure that distinctChars stays sorted, it is important to insert new chars at the correct position when updating distinctChars, which is why two calls to System.arraycopy are needed.
I did some simulations with random char arrays, each containing 100000 random characters. Here are the results of a set of 10 simulations:
With String: 9.183 seconds
With char arrays: 2.404 seconds
With String: 4.159 seconds
With char arrays: 2.075 seconds
With String: 4.721 seconds
With char arrays: 2.116 seconds
With String: 4.758 seconds
With char arrays: 2.056 seconds
With String: 4.517 seconds
With char arrays: 2.056 seconds
With String: 4.707 seconds
With char arrays: 2.038 seconds
With String: 4.803 seconds
With char arrays: 2.049 seconds
With String: 4.706 seconds
With char arrays: 2.024 seconds
With String: 4.683 seconds
With char arrays: 2.045 seconds
With String: 4.549 seconds
With char arrays: 2.052 seconds
I have no idea why the first simulation with the String algorithm takes twice as long as all the others. It was like that every time I ran the program, even when I switched the order of the two tests (meaning the first String algorithm still took twice as long as the others, even when the char[] algorithm was tested first). Maybe I did something wrong, or the JVM does something mysterious here.
Apparently, the larger the original char array, the greater the difference between the performance of the two algorithms. Here are the results of 10 simulations with 500000 random characters:
With String: 20.44 seconds
With char arrays: 4.474 seconds
With String: 21.344 seconds
With char arrays: 3.489 seconds
With String: 22.064 seconds
With char arrays: 3.315 seconds
With String: 22.155 seconds
With char arrays: 3.351 seconds
With String: 22.325 seconds
With char arrays: 3.386 seconds
With String: 22.149 seconds
With char arrays: 3.335 seconds
With String: 22.175 seconds
With char arrays: 3.352 seconds
With String: 22.16 seconds
With char arrays: 3.343 seconds
With String: 22.502 seconds
With char arrays: 3.362 seconds
With String: 22.122 seconds
With char arrays: 3.351 seconds
This should do what you want:
ArrayList<char> charList = new ArrayList<char>(0);
for (int i= 0; i < b.length; i++) {
if (b[i] == condition) {
charList.Add(b[i]);
}
}
charList.toArray();
Because comments don't allow good code formatting:
At the beginning of your code, you get the String contents as a char[] and immediately lose it again by assigning a new char[] of the same size to the variable.
char[] b = inputString.toCharArray();
b = new char[b.length];
so the loop after works on a default-initialized array, not on the string contents. You need two array references to do the copying.
A char is promoted to an int, which takes precedence over autoboxing, so remove(int) is called instead of remove(Object) you may have intuitively expect.
You can force the "right" method to be called by boxing the argument yourself:
chars.remove(Character.valueOf('a'));
You need to cast it to an object type to force the compiler to choose remove(Object) instead of remove(int):
chars.remove((Character) 'a');
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.
You have to use replaceAll instead of split, for example :
"0888888888 0888123456 +359886001122".replaceAll("\\+", "");
this will show you :
0888888888 0888123456 359886001122
//-------------------^------------
Then if you want to split each number you can use split(" ") like this :
String numbers[] = "0888888888 0888123456 +359886001122".replaceAll("\\+", "").split(" ");
System.out.println(Arrays.toString(numbers));
this will give you :
[0888888888, 0888123456, 359886001122]
EDIT
Or like @shmosel said in comment you ca use replace("+", "")
You can replace it with
var lv_numbers = "0888888888 0888123456 +359886001122".replace('+','');
Say I have one char* and one char[] like this:
char* letter = "A";
char letters[] = "ABC";
How would I remove the "letter" from the "letters" array? I'm pretty new to C, so maybe this is really simple and I'm just not getting it.
Thanks!