0

Good day,

I have to create a dynamic array class which will represent a string array with dynamic size. I have created the add method and now I have some difficulties with the removeElement method.

Here is my code:

public class DynamicArray {

public String[] dynamicString = new String[0];

public void addElement(String input) {
    int loc = findFreeSpace();

    if (loc >= 0) {
        dynamicString[loc] = input;
    }
    else {
        dynamicString = Arrays.copyOf(dynamicString, dynamicString.length + 1);
        dynamicString[dynamicString.length - 1] = input;
    }

}

public void removeElement(String input){
    for(String eachElement : dynamicString){
        if(eachElement.equalsIgnoreCase(input)) ; eachElement = null;
    }
}

private int findFreeSpace() {
    for (int i = 0; i < dynamicString.length; i++) {
        if (dynamicString[i] == null) {
            return i;
        }
    }
    return -1;
}

public static void main(String[] args){
    DynamicArray array = new DynamicArray();

    array.addElement("a");
    array.addElement("b");
    array.addElement("c");
    array.addElement("d");

    array.removeElement("a");
    System.out.println(array.dynamicString[0]);
}

In the main method 4 String elements are added to the array object. I am using the removeElement method afterwards to remove a particular string element from that object's dynamicString. However, this method fails to work and the system.out.println prints "a" (instead of null) in the console.

4
  • 1
    Is there any particular reason you're doing this instead of using an ArrayList? Commented Apr 13, 2014 at 10:18
  • 1
    You made a typo. Remove the ; before eachElement = null; in if(eachElement.equalsIgnoreCase(input)) ; eachElement = null; (note that this by itself doesn't solve your problem, look at @JBNizet's answer) Commented Apr 13, 2014 at 10:18
  • 1
    @Human I guess that the idea is to learn about arrays. I'm a bit partial against lessons that force the student to use a method that should never be used in real life though. You learn to use an array but now you have to unlearn that an array can be used as a list. Hopefully the next lesson... Commented Apr 13, 2014 at 10:26
  • thanks fellas, Nizet's answer has fixed my problem Commented Apr 13, 2014 at 10:41

4 Answers 4

2

eachElement is a copy of the reference in the array, and you're assigning a new value (null) to this copy. So the original reference is left unmodified. You need a traditional loop, which modifies dynamicString[i]:

for (int i = 0; i < dynamicString.length; i++){
    if (dynamicString[i].equalsIgnoreCase(input)) {
        dynamicString[i] = null;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should remove the ";" after the if.

Also, as said in the comments you are modifying only the local variable. Use a for loop instead of a foreach loop to iterate through the index of your array and then once you've got the index of the element you want to change you can update your array

1 Comment

That statement is correct but it doesn't solve the problem as he's still not modifying the array - he's only modifying a local variable eachElement.
0

Your code is equivalent to this:

for(Iterator<String> it = dynamicString.iterator; it.hasNext(); ){
    String eachElement = it.next();
    if(eachElement.equalsIgnoreCase(input)) ;
    eachElement = null;
}

Can you see why it won't work?

Comments

0

Multiple problems

1.';' after if means eachElement = null gets executed for every iteration

2.eachElement = null will not have no affect on array it is a seperate reference, you should use old style for loop in this case and access array element using index.

public void removeElement(String input){
    for(String eachElement : dynamicString){
        if(eachElement.equalsIgnoreCase(input)) ; eachElement = null;
    }
}

Other best option is to use ArrayList.

1 Comment

thanks mate, I know ArrayList is better but this is a mini-assignment in which I have to create a dynamicArray

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.