【问题标题】:Why is my if statement not returning the correct result?为什么我的 if 语句没有返回正确的结果?
【发布时间】:2014-07-18 01:37:56
【问题描述】:

我的代码结构有什么问题导致我出错吗? 我想我修复了我的 for 循环和参数,现在一切都很好,但我仍然遇到错误?

  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-1);

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

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

我得到了什么:

测试 ArrayUtils 类异常处理 - 2/07/2014

--- 测试 minValue 方法---

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

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

获取 minVal() 的:[10] 好的 - 预期 minValue 返回 10 并得到:10

获取 minVal() 的:[20,30] 好的 - 预期 minValue 返回 20 并得到:20

获取 minVal() 的:[40,30] 好的 - 预期 minValue 返回 30 并得到:30

获取 minVal() 的:[10,10] 好的 - 预期 minValue 返回 10 并得到:10

获取 minVal() 的:[15,25,35] 好的 - 预期 minValue 返回 15 并得到:15

获取 minVal() 的:[25,20,30] 好的 - 预期 minValue 返回 20 并得到:20

获取 minVal() 的:[50,40,30] 好的 - 预期 minValue 返回 30 并得到:30

获取 minVal() 的:[50,-10,40,-25,50,40,30] 好的 - 预期 minValue 返回 -25 并得到:-25

获取 minVal() 的:[200,50,-40,60,-15,30,75] 好的 - 预期 minValue 返回 -40 并得到:-40

--- 测试copyRange方法---

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

获取 copyRange(list,-1) of: [] OK - copyRange 抛出异常 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

获取 copyRange(list,0) of: [] 好的 - copyRange 将数组正确复制为 []

获取copyRange(list,1) of: [] OK - copyRange 抛出异常 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1

获取 copyRange(list,-1) of: [20] OK - copyRange 抛出异常 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

获取 copyRange(list,0) of: [20] 好的 - copyRange 将数组正确复制为 [20]

获取copyRange(list,1) of: [20] 好的 - copyRange 将数组正确复制为 []

获取copyRange(list,2) of: [20] 好的 - copyRange 抛出异常 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2

获取 copyRange(list,-1) of: [10,15] OK - copyRange 抛出异常 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

获取 copyRange(list,0) of: [10,15] 好的 - copyRange 将数组正确复制为 [10,15]

获取 copyRange(list,1) of: [10,15] 好的 - copyRange 将数组正确复制为 [15]

获取 copyRange(list,2) of: [10,15] 好的 - copyRange 将数组正确复制为 []

获取 copyRange(list,3) of: [10,15] OK - copyRange 抛出异常 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3

获取copyRange(list,-1) of: [30,35,40,45,50] OK - copyRange 抛出异常 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

获取copyRange(list,0) of: [30,35,40,45,50] OK - copyRange 将数组正确复制为 [30,35,40,45,50]

获取 copyRange(list,1) of: [30,35,40,45,50] 好的 - copyRange 将数组正确复制为 [35,40,45,50]

获取 copyRange(list,2) of: [30,35,40,45,50] 好的 - copyRange 将数组正确复制为 [40,45,50]

获取 copyRange(list,3) of: [30,35,40,45,50] 好的 - copyRange 将数组正确复制为 [45,50]

获取 copyRange(list,4) of: [30,35,40,45,50] 好的 - copyRange 将数组正确复制为 [50]

获取 copyRange(list,5) of: [30,35,40,45,50] 好的 - copyRange 将数组正确复制为 []

获取 copyRange(list,6) of: [30,35,40,45,50] OK - copyRange 抛出异常 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 6

--- 测试 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

完成 - 按回车键结束程序

这些都应该没问题而不是错误 我是否错误地处理了这段代码? 抱歉,我之前问过这个问题,但方式不正确

【问题讨论】:

  • int last = (list.length-1); 也许?
  • 我认为您做错的关键是未能使用调试器和/或日志记录/跟踪来调试您的代码。

标签: java arrays debugging


【解决方案1】:

您缺少的一件事是对lastIndexOf 中的数组进行空检查。 但主要的错误是,一旦找到第一个匹配项,您就不会跳出循环(这两种方法都是如此)。

哦,int indexValue = 0; 是错误的。您应该将其初始化为 -1(因为这是表示“未找到”的值)。

考虑到所有这些因素,我认为这应该可行:

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

        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;
                break;
            }
        }
        return indexValue;
    }

    public static int lastIndexOf(int[] list, int searchValue) throws BadArrayException
    {
        int indexValue = -1;

        if(list == null)
            throw new BadArrayException("Array is null");

        int last = (list.length-1);

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

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

【讨论】:

    【解决方案2】:

    代码存在三个问题,

    • 缺少对 lastIndexOf 函数中名为 list 的参数的 NULL 验证。如果您汇总该验证,您将避免错误:

    错误 - lastIndexOf 引发了意外异常:java.lang.NullPointerException

    • 对于这两个函数,您必须使用 -1 而不是 0 初始化 var indexValue,以避免:

    错误 - 预期 indexOf 返回 -1 但得到:0

    • 对于这两个函数,一旦找到searchValue 的第一次出现,就必须返回索引i。通过此更改,您将避免以下错误:

    错误 - 预期 indexOf 返回 2 但得到:8

    通过这三个更改,您的代码必须如下所示:

    public static int indexOf(int[] list, int searchValue) throws BadArrayException 
    {
        int indexValue = -1;
    
        if(list == null)
            throw new BadArrayException("Array is null");
    
        //Not neccessary
        /*else if((list.length) == 0)
            return -1;*/
    
        for(int i = 0; i < (list.length); i++){
            if(list[i] == searchValue)
                return i;
        }
        return indexValue;
    }
    
    public static int lastIndexOf(int[] list, int searchValue) throws BadArrayException
    {
        int indexValue = -1;
    
        if(list == null)
            throw new BadArrayException("Array is null");
    
        int last = (list.length-1);
    
        //Not neccessary
        /*if(list.length == 0)
            return -1;*/
    
        for(int i = last; i >= 0; i--){
            if(list[i] == searchValue)
                return i;
        }
        return indexValue;        
    }
    

    请注意,如果 indexValue 初始化为 -1,则不再需要您在代码中进行的某些验证。

    最后我推荐你使用一些调试工具或者用纸笔运行你的代码,找出错误的原因。

    希望能帮到你。 问候!

    【讨论】:

      【解决方案3】:

      当你找到值时,你应该跳出循环:

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

      【讨论】:

        最近更新 更多