【问题标题】:Is the for loop what is causing the error in this code?for 循环是导致此代码错误的原因吗?
【发布时间】:2014-07-17 23:49:12
【问题描述】:

所以对于我的这部分代码,我想知道为什么我的测试类不起作用。我完全按照说明进行操作,但我不确定我为什么会出错。 基本上对于这两个部分, 如果数组参数为空,它必须抛出带有消息“数组为空”的 BadArrayException。 如果数组参数的长度为 0,它必须返回 -1。 它不得更改数组参数内容,也不得将整个数组参数内容复制到另一个数组。 要找到第一个匹配项,它必须搜索一次数组参数。 它不读取或打印任何内容。

那么我应该首先解决 intvalue=0 的问题吗?还是我的 for 循环有问题?或者在第二个循环中,我不应该将 int last 作为 list.length 吗?

 public static int indexOf(int[] list, int searchValue) throws BadArrayException 
    {
        int indexValue = 0;

        if(list == null)
            throw new BadArrayException("Array is null");
        else if(list.length == 0)
            return -1;

        for(int i = 0; i < list.length; i++){
            if(list[i] == searchValue)
                indexValue = i;
        }
        return indexValue;
    }

    public static int lastIndexOf(int[] list, int searchValue) throws BadArrayException
    {
        int indexValue = 0;
        int last = list.length;

        if(list.length == 0)
            return -1;

        for(int i = last; i >= 0; i--){
            if(list[i] == searchValue)
                indexValue = i;
        }
        return indexValue;        
    }
}

我应该得到这个作为我的结果,但我没有,我一直在这部分得到意外的错误。谢谢。

--- Testing indexOf and lastIndexOf method ---

Getting indexOf of a null array
  OK - indexOf threw exception for null array: BadArrayException: Array is null

Getting lastIndexOf of a null array
  OK - lastIndexOf threw exception for null array: BadArrayException: Array is null

Getting lastIndexOf(5) of: []
  OK - expected lastIndexOf to return -1 and got: -1

Getting indexOf(5) of: [5,10,5,15,5]
  OK - expected indexOf to return 0 and got: 0

Getting indexOf(0) of: [5,10,5,15,5]
  OK - expected indexOf to return -1 and got: -1

Getting indexOf(15) of: [5,10,5,15,5]
  OK - expected indexOf to return 3 and got: 3

这就是我得到的 --- 测试 indexOf 和 lastIndexOf 方法 ---

获取空数组的 indexOf 好的 - indexOf 为空数组抛出异常:BadArrayException

获取空数组的lastIndexOf 错误 - lastIndexOf 引发意外异常:java.lang.NullPointerException

获取 indexOf(5) of: [] 好的 - 预期 indexOf 返回 -1 并得到:-1

获取 lastIndexOf(5) of: [] 好的 - 预期 lastIndexOf 返回 -1 并得到:-1

获取 indexOf(20) of: [20] 好的 - 预期 indexOf 返回 0 并得到:0

获取 indexOf(25) of: [20] 错误 - 预期 indexOf 返回 -1 但得到:0

获取 lastIndexOf(20) of: [20] 好的 - 预期 lastIndexOf 返回 0 并得到:0

获取 lastIndexOf(25) of: [20] 错误 - 预期 lastIndexOf 返回 -1 但得到:0

获取 indexOf(5) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 indexOf 返回 0 但得到:6

获取 indexOf(10) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 indexOf 返回 1 但得到:7

获取 indexOf(15) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 indexOf 返回 2 但得到:8

获取 indexOf(20) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 indexOf 返回 3 但得到:9

获取 indexOf(0) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 indexOf 返回 -1 但得到:0

获取 lastIndexOf(5) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 lastIndexOf 返回 6 但得到:0

获取 lastIndexOf(10) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 lastIndexOf 返回 7 但得到:1

获取 lastIndexOf(15) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 lastIndexOf 返回 8 但得到:2

获取 lastIndexOf(20) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 lastIndexOf 返回 9 但得到:3

获取 lastIndexOf(0) of: [5,10,15,20,10,15,5,10,15,20] 错误 - 预期 lastIndexOf 返回 -1 但得到:0

完成 - 按回车键结束程序

【问题讨论】:

  • 问题:for(int i = last; i >= 0; i--) 应该更改为 for(int i = last -1 ; i >= 0; i--) 索引开始从 0
  • 您向我们展示了应该发生的事情。究竟发生了什么?
  • 如果您尝试查找某项的第一个索引,那么当您找到您要查找的内容时,停止查找
  • 说明没有这样说,但看起来你应该在任何时候找不到你正在寻找的值时返回 -1。您需要确保这样做。

标签: java arrays debugging for-loop


【解决方案1】:

让我们从indexOf开始。

找到要搜索的数字后,将索引存储在一个变量中,然后在完成后返回该变量。让我们看看[5,10,5,15,5] 测试中发生了什么:

  • 测试list[0] == 5。没错,所以indexValue = 0

理想情况下,此时我们应该返回0。但事实并非如此:

  • 测试list[1] == 5。错,所以什么都不做。
  • 测试list[2] == 5。没错,所以indexValue = 2
  • 测试list[3] == 5。错,所以什么都不做。
  • 测试list[4] == 5。没错,所以indexValue = 4
  • 返回4。不是0,应该如此。

事实证明,indexOf 的工作方式与 lastIndexOf 应该一样。不要返回最后一次找到值的索引,而是在找到值后立即返回索引。

lastIndexOf 有同样的问题,但它还有另一个问题,就是你如何初始化last。假设我们有一个 1 元素数组,如下所示:

[1]

这个数组的最后一个索引是0,但是你用list.length初始化last,这是1,而不是0。你必须减去 1。

【讨论】:

    【解决方案2】:

    问题在于您对last 的初始化。

    采用StringSAMPLE"SAMPLE".toCharArray().length 的值为 5,但在循环中,您希望从 "SAMPLE".toCharArray()[4] 循环到 "SAMPLE".toCharArray()[0]

    即将last 更改为"SAMPLE".toCharArray().length - 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 2014-01-17
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      相关资源
      最近更新 更多