Java char Array - deleting elements

arrays, char, java

Solution

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 simpler`Arrays.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);

Problem

In Java, I want to delete certain elements from a char array so it does something like: ``` char[] Array1 = {'h','m','l','e','l','l'}; Array1 = //character index[2] to character index[5] ``` How can this be done?

Original source

Related problems