遍历数组在 if 语句内部。

huangapple go评论62阅读模式
英文:

Iterating over an array inside an if statement

问题

我正在构建具有可定制规则的生命游戏。我从函数中收到一个数字数组,需要遍历该数组以获取一组数字。我需要这些数字添加到一个if语句中,而不需要知道数组的长度,以便更新生命游戏的规则。以下是代码示例:

if(nAlive < 2)
  next[i][j] = false;
else if(nAlive == 2 || nAlive == 3)
  next[i][j] = true;
else
  next[i][j] = false;

我需要的是(这是伪代码):

if(nAlive == surviving[0] || nAlive == surviving[1] || ... nAlive == surviving[n])
  next[i][j] = true;
else
  next[i][j] = false;
英文:

I am building the game of life with customizable rules. I am receiving an array of numbers from a function and I need to iterate over the array in order to get a collection of numbers. I need those numbers to add to an if statement without knowing the length of the array in order to update the rules for the game of life. Here is the example for the code:

if(nAlive &lt; 2)
  next[i][j] = false;
else if(nAlive == 2 || nAlive == 3)
  next[i][j] = true;
else
  next[i][j] = false;

What I need is (this is in pseudocode):

if(nAlive == surviving[0] || nAlive == surviving[1] || ... nAlive == surviving[n])
  next[i][j] = true;
else
  next[i][j] = false;

答案1

得分: 1

答案是:

if alive[i][j]:
    next[i][j] = False
    for x in range(len(surviving)):
        if surviving[x] == nAlive:
            next[i][j] = True
英文:

The Answer is:

if(alive[i][j])
                {
                    next[i][j] = false;
                    for (int x = 0; x &lt; surviving.length; x++) {
                        if (surviving[x] == nAlive)
                            next[i][j] = true;
                    }
                }

答案2

得分: 1

尝试一下:

next[i][j] = Arrays.stream(surviving).anyMatch(s == nAlive);
英文:

Try this:

next[i][j] = Arrays.stream(surviving).anyMatch(s == nAlive);

答案3

得分: 1

我的建议是按以下方式将这个矩阵封装为对象:

public class GameOfLife {
    private Collection<Collection<Integer>> alive;
    private int numberOfCorrectValues;

    public void addNewValue(int rowIdx, int colIdx, int value){
        if(isMakeTheRules(alive.get(rowIdx).get(colIdx)) && numberOfCorrectValues > 0) numberOfCorrectValues--;
        if(valueIsMakeTheRules(value)) numberOfCorrectValues++;
        setAliveWithNewValue(rowIdx, colIdx, value);
    }

    public boolean isTheMatrixMakeTheRules(){
        return numberOfCorrectValues > 0;
    }
}

setAliveWithNewValue方法改变集合中的值时(你需要实现它),而isMakeTheRules是检查每个条目的方法。

这个想法是每次保存矩阵的状态,只计算一次大的条件语句,将复杂度从O(n) 降低到 O(1)

英文:

My suggestion is wrap this matrix by object in the next way:

public GameOfLife {
    private Collection&lt;Collection&lt;int&gt;&gt; alive;
    private int numberOfCorrectValues;

    public void addNewValue(int rowIdx, int colIdx, in value){
        if(isMakeTheRules(alive.get(rowIdx).get(colIdx)) and numberOfCorrectValues &gt; 0) numberOfCorrectValues--;
        if((value) isMakeTheRules) numberOfCorrectValues++;
        setAliveWithNewValue(int rowIdx, int colIdx, in value);

    public boolean isTheMatrixMakeTheRules(){
        return numberOfCorrectValues &gt; 0;
    }
}

when setAliveWithNewValue is change the value in the collection (you should impl it), and isMakeTheRules is method that check per one entry. <br>

the idea is to save the state of the matrix every time and to calculate the big if only once and reduce the complexity from O(n) to O(1)

huangapple
  • 本文由 发表于 2020年10月7日 03:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64232880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定