0

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

6
  • what does delete variable stand for? Commented Sep 20, 2013 at 5:43
  • Please show a complete example. What are the types of delete and word. Commented Sep 20, 2013 at 5:44
  • Edited* hopefully that might help a bit Commented Sep 20, 2013 at 5:51
  • You have to use either char array and then create a new string or get beforeString+afterString.. something like .. String beforeString = myString.substring(0,num); String afterString = myString.substring(num+1,myString.length()-beforeString.length()-1) Commented Sep 20, 2013 at 6:07
  • Is "word" a String, or a character array? Commented Sep 20, 2013 at 6:53

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

0

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.
    }
}

3 Comments

That makes senses and I tried it Gennaro but with: word[i] = null, i cannot convert from null to char
Sorry, you're right. Was thinking of C. You can set it to the default value of character instead: '\u0000'.
From your clarification, the other way around that would be to copy the array over. Make a temp array of size word.length. Copy the word array to the temp array. Then say word = new char[temp.length-1]. That way, you can copy over all the values except the one you want to remove.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.