Bravo
Bravo

Reputation: 1139

Cannot assign string to null within a string array

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.

Upvotes: 0

Views: 739

Answers (4)

Puru--
Puru--

Reputation: 1111

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.

Upvotes: 0

user2137101
user2137101

Reputation:

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

Upvotes: 1

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

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?

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

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

Upvotes: 2

Related Questions