6

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1516514
  • 95
  • 1
  • 2
  • 4

5 Answers5

12

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);
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • how do u copy certain elements coz i want: 'code'(char[] example = //first character to 3rd last.) is there a range method or something? – user1516514 Jul 11 '12 at 03:35
2

You can use Arrays.copyOfRange like this:

array1 = Arrays.copyOfRange(array1, 2, 5);

More info

higuaro
  • 15,730
  • 4
  • 36
  • 43
2

Java function to remove a character from a character array:

String msg = "johnny can't program, he can only be told what to type";
char[] mychararray = msg.toCharArray();
mychararray = remove_one_character_from_a_character_array_in_java(mychararray, 21);
System.out.println(mychararray);

public char[] remove_one_character_from_a_character_array_in_java(
                           char[] original, 
                           int location_to_remove)
{
    char[] result = new char[original.length-1];
    int last_insert = 0;
    for (int i = 0; i < original.length; i++){
        if (i == location_to_remove)
            i++;

        result[last_insert++] = original[i];
    }
    return result;
}

The above method prints the message with the index 21 removed. You could place this in a loop to remove multiple items. Technically you are not deleting an item, you are creating a brand new char array with the item removed. You have to step through the entire string for each remove which is very inefficient.

Delete a character by index from a character array with StringBuilder in Java:

String mystring = "inflation != stealing";
char[] my_char_array = mystring.toCharArray();
StringBuilder sb = new StringBuilder();
sb.append(mystring);
sb.deleteCharAt(10);
my_char_array = sb.toString().toCharArray();
System.out.println(my_char_array);             //prints "inflation = stealing"

The above code removes the exclamation mark from the character array. If you want to delete a RANGE of characters, use sb.delete(10, 15);

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
0

You can use this method:

public static char[] copyArray(char[] original, int start, int length)
{
    char[] result = new char[length];
    for (int i = 0; i < length; i++)
    {
        result[i] = original[start + i];
    }
    return result;
}
Brad
  • 9,113
  • 10
  • 44
  • 68
  • 5
    Or you could use [`Arrays.copyOfRange(char[], int, int)`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOfRange\(char[],%20int,%20int\)). – Paul Bellora Jul 11 '12 at 03:48
0

Yes, you can use Arrays.copyOfRange(char[], starting index, ending index) method for you.

You can also view this --

http://www.codingdiary.com/developers/developers/diary/javaapi/java/util/SampleCode/CopyofRange8ArrayExampleCode.html

Kumar Shorav
  • 531
  • 4
  • 16