0

The method is not returning the value of the local variable.

Can I use the value of local variable index from the following method

public boolean contains(Object input) {
    int index = 0;

    while(myAsetIterator.hasNext()) {
        index++;
        if(input.equals(myAsetIterator.next())) {
            return true;
        }
    }
    return false;
}

in this method as the index of the array of the object that I want to remove.

public boolean remove(Object o) {
    int count = 0;
    if(o == null) {
        return false;
    }
    if(contains(o)) {
        genArray[index] == null;
    }
    if (count > 0) {
        System.out.println(count+" same elements were present in Aset. "
                + "Removed all those "+count+" elements from Aset.");
        return true;
    }
    return false;
}

I know the scope of a local variable is limited to the method it's declared in. But there might be a way that I might not now yet to make this happen without using a field/instance variable.

8
  • 1
    I'm afraid you can't get the local variable value. You may look here : stackoverflow.com/questions/6816951/… but that would need the library to be compiled with "-g" option. Commented Mar 13, 2015 at 7:59
  • Why cant you declare index as a class variable (make it static) Commented Mar 13, 2015 at 8:03
  • 4
    if contains() was named indexOf() and returned the index, instead of returning a boolean, you could get the index by calling indexOf(), and then use it to set the value at this index to null. Commented Mar 13, 2015 at 8:03
  • 1
    @ClydeD'Cruz that's probably the worst advice to give. Why would two instances share the same index? Commented Mar 13, 2015 at 8:05
  • Oh I may not understood, my comment suppose you can't modify the method code. Commented Mar 13, 2015 at 8:07

1 Answer 1

5

No. The whole point of it being local to a method is that it only exists within that method. The options are:

  • Use an instance field, i.e. make it part of the state of the object. That's unlikely to be appropriate.
  • Use a static field, i.e. make it part of the static of the type. That's almost certainly inappropriate.
  • Change the existing method to return the information you want.
  • Create a new method to return the information you want.
  • Duplicate the existing code within remove so that you can get the index. That would be sad :(

As an example of the last two, you could write:

public int indexOf(Object input) {       
    int index = 0;    
    while(myAsetIterator.hasNext()) {
        index++;
        if (input.equals(myAsetIterator.next())) {
            return index;
        }
    }
    return -1;
}

public boolean contains(Object input) {
    return indexOf(input) == -1;
}

... then in your remove method, you'd use indexOf instead of contains.

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

2 Comments

Just a small correction, in case there's a match, you're not returning updated index , always returning -1 :)
@Arkantos: Fixed, thanks. (In fact it would have failed to compile :)

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.