简体   繁体   中英

Looping through multiple variables in Java

I am trying to figure out how to loop through and check if the conditions are met for the player to be in any of the variables (TEST1, TEST2, etc), using the isIn methods that I created. However, I am stuck on how I would do this without manually creating a bunch of if else statements.

    public class Boundary {

    int minX, minY, highX, highY;

    public Boundary(int minX, int highX, int minY, int highY) {
        this.minX = minX;
        this.minY = minY;
        this.highX = highX;
        this.highY = highY;
    }

    public int getMinimumX() {
        return minX;
    }

    public int getMinimumY() {
        return minY;
    }

    public int getMaximumX() {
        return highX;
    }

    public int getMaximumY() {
        return highY;
    }

    //current x, y - placeholder variables
    public int x, y;

    public boolean isIn(Boundary[] boundaries) {
        for (Boundary b : boundaries) {
            if (x >= b.minX && x <= b.highX && y >= b.minY && y <= b.highY) {
                return true;
            }
        }
        return false;
    }

    public boolean isIn(Boundary boundaries) {
        return x >= boundaries.minX && x <= boundaries.highX && y >= boundaries.minY && y <= boundaries.highY;
    }

    //low x, high x, low y, high y  
    private final Boundary TEST1 = new Boundary(2368, 2392, 9479, 9498);
    private final Boundary TEST2 = new Boundary(2328, 3492, 5279, 1498);
    private final Boundary[] TEST3 = new Boundary[] {
        new Boundary(2817, 2917, 3192, 3204), new Boundary(2817, 2961, 3131, 3191)
    };
    private final Boundary[] TEST4 = new Boundary[] {
        new Boundary(1817, 6917, 5192, 3204), new Boundary(5817, 3961, 6131, 4191)
    };
    private final Boundary TEST5 = new Boundary(8328, 9492, 7279, 6498);
    private final Boundary TEST6 = new Boundary(1328, 2492, 3279, 4498);
}

Currently only way I can figure out how to do this is:

public void isInAny() {
    if (isIn(TEST1)) {
        System.out.println("in test1");
    } else if (isIn(TEST2)) {
        System.out.println("in test2");
    } else if (isIn(TEST3)) {
        System.out.println("in test3");
    } else if (isIn(TEST4)) {
        System.out.println("in test4");
    } else if (isIn(TEST5)) {
        System.out.println("in test5");
    } else if (isIn(TEST6)) {
        System.out.println("in test6");
    }
    System.out.println("not in any tests");
}

Is there any easier way I can loop through this without making a bunch of if else statements? Any help is appreciated.

Thank you!

You can get the fields and loop through them:

Field[] fields = Boundary.getFields();
for (Field f : fields){
    if ((f instanceof Boundary)||((f.getType().isArray())&&(f[0] instanceof Boundary))){
        if (isIn(f)){
            System.out.println("in " + f.getName());
        }
    }
}  

Actually you can use Java reflection and by the name of the field you can retrieve field value but I don't see the sense to use it here. if you want something more beautiful you can try something like this. But I recommend to leave it as is

int minX, minY, highX, highY;
List<Boundaries> boundaries = new LinkedList<>();

{
    boundaries.add(new Boundaries(() -> System.out.println("test1"), new Boundary(2368, 2392, 9479, 9498)));
    boundaries.add(new Boundaries(() -> System.out.println("test2"), new Boundary(2328, 3492, 5279, 1498)));
}

public Boundary(int minX, int highX, int minY, int highY) {
    this.minX = minX;
    this.minY = minY;
    this.highX = highX;
    this.highY = highY;
}

public int getMinimumX() {
    return minX;
}

public int getMinimumY() {
    return minY;
}

public int getMaximumX() {
    return highX;
}

public int getMaximumY() {
    return highY;
}

//current x, y - placeholder variables
public int x, y;

public boolean isIn(Boundary[] boundaries) {
    for (Boundary b : boundaries) {
        if (x >= b.minX && x <= b.highX && y >= b.minY && y <= b.highY) {
            return true;
        }
    }
    return false;
}


public void isInAny() {
    for (Boundaries boundary : boundaries) {
        isIn(boundary.boundaries) {
            boundary.action.test();
        }
    }
}

interface Action {
    void test();
}

static class Boundaries {
    Boundary[] boundaries;
    Action action;

    public Boundaries(Action action, Boundary... boundary) {
        this.boundaries = boundary;
        this.action = action;
    }

}

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