user2172295
user2172295

Reputation: 121

Delete character in certain position in array

What I am looking to do is delete a character at a position in an array. The array is called word.

removecharacter is an int

word is a an array that was formed from a string

There is a test program already written where the person can type in an int (removecharacter) which will give the position as to where in the array to delete the item

I think I am on the right track but am unsure on a certain line, which is the actual delete line. Any tips?

public boolean delCharAt(int removecharacter) {
    if (removecharacter <= word.length && delete >= 0) 
    {

        //I wish to delete the character at removecharacter

    }

Any help on where to go from here? Thanks

Upvotes: 0

Views: 403

Answers (4)

Gennaro De Luca
Gennaro De Luca

Reputation: 108

One way to accomplish this task would be to move down all values after the deleted value. You could then set the new empty slot to null.

if(removecharacter <= word.length && removecharacter >= 0)
{
    for(int i=removecharacter+1; i<word.length; i++) {
         word[i-1] = word[i];
         word[i] = '\u0000'; // This will make sure that no duplicates are created in this                     
                             // process.
    }
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

I'm with everybody else who says to use something like an ArrayList, but if you have no choice, you can use System.arraycopy to copy the contents from the original array to the a new, temporary array and assign the result back to the original (word).

This will decrease the size of the array as well as remove the character...

public class ArrayDelete {

    // This is because I'm to lazy to build the character
    // array by hand myself...
    private static String text = "This is an example of text";
    private static char word[] = text.toCharArray();

    public static void main(String[] args) {
        System.out.println(new String(word));
        delete(9);
        System.out.println(new String(word));
    }

    public static void delete(int charAt) {
        if (word.length > 0 && charAt >= 0 && charAt < word.length) {
            char[] fix = new char[word.length - 1];

            System.arraycopy(word, 0, fix, 0, charAt);
            System.arraycopy(word, charAt + 1, fix, charAt, word.length - charAt - 1);

            word = fix;
        }
    }

}

This example outputs...

This is an example of text
This is a example of text

Upvotes: 0

Silviu Burcea
Silviu Burcea

Reputation: 5348

If you remove elements inside an array, you should consider using a (Array)List. You will have a method to remove an object from the list or an element at an index. Try to avoid reinventing the wheel.

Here is the Javadoc: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Also, because your word comes from a String, you can use StringBuilder, you have an method called deleteCharAt.

Upvotes: 4

Ajinkya Dahale
Ajinkya Dahale

Reputation: 36

If you want the versatility of addition and removal, you might consider using ArrayList or List instead. They both have inbuilt functions for that task.

If you absolutely have to use arrays, you would also have to store a value for length of the array that i used.

Upvotes: 1

Related Questions