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?

0

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

how do u copy certain elements coz i want: 'code'(char[] example = //first character to 3rd last.) is there a range method or something?
2

You can use Arrays.copyOfRange like this:

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

More info

Comments

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);

1 Comment

how could we do this with O(1) space?
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;
}

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.