简体   繁体   中英

How to write a recursive function using static variables

I am trying to write a code to get the set of points (x,y) that are accessible to a monkey starting from (0,0) such that each point satisfies |x| + |y| < _limitSum. I have written the below code and have used static HashSet of members of Coordinate type (not shown here) and have written a recursive method AccessPositiveQuadrantCoordinates. But the problem is the members of the HashSet passed across the recursive calls is not reflecting the Coordinate members added in previous calls. Can anybody help me on how to pass Object references to make this possible? Is there some other way that this problem can be solved?

public class MonkeyCoordinates {
public static HashSet<Coordinate> _accessibleCoordinates = null;
private int _limitSum;
public MonkeyCoordinates(int limitSum) {
    _limitSum = limitSum;
    if (_accessibleCoordinates == null)
        _accessibleCoordinates = new HashSet<Coordinate>();
}
public int GetAccessibleCoordinateCount() {
    _accessibleCoordinates.clear();
    Coordinate start = new Coordinate(0,0);
    AccessPositiveQuadrantCoordinates(start);
    return (_accessibleCoordinates.size() * 4);
}
private void AccessPositiveQuadrantCoordinates(Coordinate current) {
    if (current.getCoordinateSum() > _limitSum) { return; }
    System.out.println("debug: The set _accessibleCoordinates is ");
    for (Coordinate c : _accessibleCoordinates) {
        System.out.println("debug:" + c.getXValue() + " " + c.getYValue());
    }

    if (!_accessibleCoordinates.contains(current)) { _accessibleCoordinates.add(current); }
    AccessPositiveQuadrantCoordinates(current.Move(Coordinate.Direction.East));
    AccessPositiveQuadrantCoordinates(current.Move(Coordinate.Direction.North));
}

I will give points to all acceptable answers.

Thanks ahead, Somnath

But the problem is the members of the HashSet passed across the recursive calls is not reflecting the Coordinate members added in previous calls.

I think that's very unlikely. I think it's more likely that your Coordinate class doesn't override equals and hashCode appropriately, which is why the set can't "find" the values.

As an aside, using static variables like this seems like a very bad idea to me - why don't you create the set in GetAccessibleCoordinateCount() and pass the reference to AccessPositiveQuadrantCoordinates , which can in turn keep passing it down in the recursive calls?

(As another aside, I would strongly suggest that you start following Java naming conventions...)

i don't see any problem with making the field _accessibleCoordinates non-static

and you should know that HashSet does not guarantee the same iteration order everytime, you could better use a LinkedList for that purpose...

and about pass by reference, i found this post very useful

java - pass by value - SO link

From what you are doing you would be updating _accessibleCoordinates in every recursive call correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM