如何根据ArrayList中的值更改一个布尔值



我正在尝试制作一个用于解决数独难题的演绎算法。我的板由一个ArrayList中的81个节点组成。-每个节点都有一个布尔值如果我的算法(称为CRME)发现至少有一个节点的布尔值(hasChanged)等于true,我希望它能继续尝试解决这个谜题,但我不确定如何做到这一点。canChange也是该方法所包含的类中的全局变量。

public void CRME() {
    canChange = true;
    while (canChange == true) {
        for (Node node : cells) {
            scanColumn(node);
            scanRow(node);
            scanMiniGrid(node);
        }
    }
}
    public void scanRow(Node n){
    for(Node node : cells){
        int arraySize = node.posVals.size();
        ArrayList<Integer> toRemove = new ArrayList<Integer>();
        if(node.get_ROW_ID() == n.get_ROW_ID()){
            toRemove.add(node.getValue());
        } 
        n.posVals.removeAll(toRemove);
        if(arraySize < node.posVals.size()){
            node.hasChanged = true;
        }
    }
}

这是scanRow方法,另外两个名称相似的方法相同,但明显的语法发生了变化,例如node.get_ROW_ID();将是node.get_COL_ID();。

我假设您有一个静态变量

static boolean hasChanged; // in the Node class

所以你可以使用:

 node.hasChanged = true;

或者你可以创建hasChange方法来设置变量,比如

boolean hasChanged;
public void hasChanged(boolean val){
    this.hasChanged = val;
}

并在循环中使用,就像这样:

hasChanged(true); or hasChanged(false);

并不是说你的方法是最好的,但如果你试图简单地继续,而hasChanged中的一个是任何节点的true,那么以下就足够了:

public void CRME() 
{
    goOn = false;
    for (Node node : yourArrayListOfNodes) 
    {
        if (node.hasChanged)
        {
           goOn = true;
           break;
        }
    }
    if (goOn)
    {
        //Insert Whatever code you want to run after the check
        //.........................................
        //Use recursion to repeat process
        //Note recursive call will only take place if goOn is true
        CRME()
    } 
}

这似乎是您想要做的,只需注意,如果您的逻辑不正确,您可以获得StackOverflowError,因为您将继续进行递归调用。

相关内容

最新更新