【问题标题】:Validating elements of a 2D array验证二维数组的元素
【发布时间】:2020-05-29 14:36:04
【问题描述】:

我用于确保元素不超过 100 或小于 0 的代码是:

boolean test = true;
for (int i = 0; i < inArray.length; i++) {
    for (int j = 0; j < inArray[0].length; j++) {
        if (inArray[i][j] <= 1 && inArray[i][j] >= 100) {
            test = false;
        }
    }
}
return test;

无论输入是什么,答案总是true

我的输入 array

{12, 1331, 23, 5, 1, 3},
{22, 23, 231, 21312, 1, 3},
{23, 31, 343, 3432, 1, 3},
{42, 12, 5454, 1212, 3, 9}

如果我输入这个 no,测试应该 = false?

学院规则意味着我不能在子模块末尾以外的任何地方使用break;return

【问题讨论】:

  • 需要注意的一点:查看您的内部 for 循环。您正在迭代哪个数组,与您正在测试哪个数组的长度?然后看看你的if 声明。密切。 &amp;&amp; 是什么意思?
  • 因为我正在测试 min > 1 和 max
  • @DaffyFarm 这不是你要测试的。你正在测试相反的结果。
  • 当您测试 (a &amp;&amp; b) 时,您正在测试 a 和 b 是否为真同时,并且没有小于 2 和同时高于 99。

标签: java arrays validation for-loop multidimensional-array


【解决方案1】:

这里有三个问题,其中两个很关键。首先,如果要遍历每个子数组,请测试 its 长度,而不是每次都测试 array[0] 的长度。其次,整数不能小于 2 并且 超过 99,因此您的条件 (inArray[i][j] &lt;= 1 &amp;&amp; inArray[i][j] &gt;= 100) 永远不会触发。

但是,这里还有更多的作用:如果您的测试纯粹是为了查看您的二维数组是否有效,请不要做不必要的工作。一旦找到一个坏元素,您就可以从字面上停止。现在,我们可以通过在for 条件句中添加一个检查来做到这一点。例如。我们可以将您的变量重命名为valid(因为您应该根据变量所代表的内容命名变量),然后我们可以将您的外循环更新为for(int i=0; valid &amp;&amp; i &lt; arr.length; i++) { ... },同时在内循环中使用相应的valid &amp;&amp; ...,但我们是不会。这段代码“做一件事”(即它“测试任何数组数组的元素有效性”),“做一件事”的代码应该在它自己的方法中,并带有描述它做什么的方法名称。这使得“不做任何我们不需要做的工作”变得更加容易:当我们知道我们已经完成了足够多的工作来产生答案时,我们就退出函数。

那么让我们这样做吧:

// 1st fix: this code "does soemthing". Do that as a method.
public boolean testForValidContent(int[][] arr) {
  // We don't need a variable to track things: because we're
  // returning a value, we can simply return the right value
  // at the moment we return.

  for(int i = 0; i < arr.length; i++) {
    for(int j = 0; j < arr[i].length; j++) { // 2nd fix: check the right array's length
      if(arri][j] <= 1 || arr[i][j] >= 100) { // 3th fix: OR, not AND

        // At this point we know the array is not valid, so there
        // is no point in running even a single thing more:
        return false;

      }
    }
  }

  // We checked the entire array, and everything's valid:
  return true;
}

【讨论】:

  • 感谢您的深入回答
  • 我已经对代码进行了一些更新,因为您应该看看这在人们编写的真实代码中应该是什么样子。
【解决方案2】:

将您的 for 循环条件更新为 i &lt; inArray.length &amp;&amp; testj &lt; inArray[0].length &amp;&amp; test,一旦发现无效号码,这将强制您的循环结束 和你的 if 条件 inArray[i][j] &lt;= 1 || inArray[i][j] &gt;= 100

boolean test = true

        for(int i = 0; i < inArray.length && test; i++){
            for(int j = 0; j < inArray[0].length && test; j++){ 
                if(inArray[i][j] <= 1 ||  inArray[i][j] >= 100){    
                    test = false;
                }
            }
        }

        return test;

您也可以跳过if 块,

boolean test = true

        for(int i = 0; i < inArray.length && test; i++){
            for(int j = 0; j < inArray[0].length && test; j++){ 
                test = (1 < inArray[i][j]  && inArray[i][j] < 100)  
            }
        }

        return test;

【讨论】:

  • 我刚刚测试了这个,无论我输入什么,我仍然得到 True
  • inArray[i][j] &lt;= 1 || inArray[i][j] &gt;= 100inArray[i][j] &lt;= 1 || inArray[i][j] &gt;= 100 哪个条件是不可能的? @Mike'Pomax'Kamermans
  • test 将始终保持设置,直到它为 false 。那么代码有什么问题?你有没有尝试过执行它? @Mike'Pomax'Kamermans
  • 啊,您添加了&amp;&amp; test... 但是为什么您的代码仍在针对inArray[0] 进行测试?这是 Java,而不是 Mathematica:二维数组是数组的数组,每个子数组可以有不同的长度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2020-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多