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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    I'm afraid you can't get the local variable value. You may look here : http://stackoverflow.com/questions/6816951/can-i-get-information-about-the-local-variables-using-java-reflection but that would need the library to be compiled with "-g" option. – Michael Laffargue Mar 13 '15 at 07:59
  • Why cant you declare index as a class variable (make it static) – Clyde D'Cruz Mar 13 '15 at 08: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. – JB Nizet Mar 13 '15 at 08:03
  • 1
    @ClydeD'Cruz that's probably the worst advice to give. Why would two instances share the same index? – JB Nizet Mar 13 '15 at 08:05
  • Oh I may not understood, my comment suppose you can't modify the method code. – Michael Laffargue Mar 13 '15 at 08:07
  • @JB Nizet - This is a project thing and I am supposed to use only the provided fields and methods inherited/implemented from an interface that the class is implementing. I am just exploring the depths of JAVA and see is there something that I might not know. Thanks for the advise though. :) – script_before_java Mar 13 '15 at 08:10
  • I can complete the remove(Object o) method using other long ways. This is just much shorter (but seems like impossible) way. :) – script_before_java Mar 13 '15 at 08:13
  • Have a look at JonSkeet's answer. You're expecting contains() to return both true and the matching index, in that case it's not appropriate to name your method as `contains()`. `indexOf()` will be your best bet where in you get some valid index for some input match and -1 in other cases – Arkantos Mar 13 '15 at 08:15

1 Answers1

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194