【问题标题】:How should I fix my code to stop getting errors with the test code?我应该如何修复我的代码以停止在测试代码中出现错误?
【发布时间】:2014-09-01 03:08:50
【问题描述】:

这个作业是关于测试数组的。这是我的代码,当运行给出的测试代码时,我们应该得到所有结果,但我得到的是我在代码下方发布的内容。 有什么建议可以解决吗? 请帮忙

    public class ArrayUtils 
{
    public static int minValue(int[] list) throws BadArrayException 
    {
        if(list == null)
            throw new BadArrayException("Array is null");
        else if(list.length == 0)
            throw new BadArrayException("Array is empty");

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


    public static int[] copyRange(int[] list, int startIndex)
            throws BadArrayException{
                if (list == null)
                    throw new BadArrayException("Array is null");
                else if (startIndex < 0 || startIndex > list.length)
                    throw new ArrayIndexOutOfBoundsException(startIndex);
                else{
                    int [] newList = new int [list.length - startIndex];
                    int x = 0;
                    int i;
                    for (i = startIndex; i < list.length; i++)
                        if (startIndex == 0){
                            newList[i] = list[i];
                        }else{
                            newList[x] = list[i];
                            x += 1;
                        }
                    }
                int[] newList = null;
                return newList;
                }

    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 == null)
            throw new BadArrayException("Array is null");
        else 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 返回一个长度为 0 的数组,但得到一个空数组

获取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 返回一个长度为 1 的数组,但得到一个空数组

获取copyRange(list,1) of: [20] 错误 - 期望 copyRange 返回一个长度为 0 的数组,但得到一个空数组

获取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 返回一个长度为 2 的数组,但得到一个空数组

获取 copyRange(list,1) of: [10,15] 错误 - 预期 copyRange 返回一个长度为 1 的数组,但得到一个空数组

获取 copyRange(list,2) of: [10,15] 错误 - 期望 copyRange 返回一个长度为 0 的数组,但得到一个空数组

获取 copyRange(list,3) of: [10,15] 好的 - 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] 错误 - 期望 copyRange 返回一个长度为 5 的数组,但得到一个空数组

获取 copyRange(list,1) of: [30,35,40,45,50] 错误 - 期望 copyRange 返回一个长度为 4 的数组,但得到一个空数组

获取 copyRange(list,2) of: [30,35,40,45,50] 错误 - 期望 copyRange 返回一个长度为 3 的数组,但得到一个空数组

获取 copyRange(list,3) of: [30,35,40,45,50] 错误 - 期望 copyRange 返回一个长度为 2 的数组,但得到一个空数组

获取 copyRange(list,4) of: [30,35,40,45,50] 错误 - 预期 copyRange 返回一个长度为 1 的数组,但得到一个空数组

获取 copyRange(list,5) of: [30,35,40,45,50] 错误 - 期望 copyRange 返回一个长度为 0 的数组,但得到一个空数组

获取 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 引发意外异常:java.lang.ArrayIndexOutOfBoundsException: 1

获取 lastIndexOf(25) of: [20] 错误 - lastIndexOf 引发意外异常:java.lang.ArrayIndexOutOfBoundsException: 1

获取 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 引发意外异常:java.lang.ArrayIndexOutOfBoundsException: 10

获取 lastIndexOf(10) of: [5,10,15,20,10,15,5,10,15,20] 错误 - lastIndexOf 引发意外异常:java.lang.ArrayIndexOutOfBoundsException: 10

获取 lastIndexOf(15) of: [5,10,15,20,10,15,5,10,15,20] 错误 - lastIndexOf 引发意外异常:java.lang.ArrayIndexOutOfBoundsException: 10

获取 lastIndexOf(20) of: [5,10,15,20,10,15,5,10,15,20] 错误 - lastIndexOf 引发意外异常:java.lang.ArrayIndexOutOfBoundsException: 10

获取 lastIndexOf(0) of: [5,10,15,20,10,15,5,10,15,20] 错误 - lastIndexOf 引发意外异常:java.lang.ArrayIndexOutOfBoundsException: 10

完成 - 按回车键结束程序

【问题讨论】:

  • 尝试使用调试器,或者至少在代码中添加一些System.out.println 调用,以确保变量或任何您认为的变量。事实上,我根本无能为力,因为没有迹象表明 copyRange(arr,start) supposed 会返回什么。另外,您似乎发布了多个错误。而且 StackOverflow 不是一个你可以向我们扔一堆代码并说“这不起作用,有什么问题?”的地方。没有任何尝试自己追查问题。对不起。

标签: java arrays debugging computer-science


【解决方案1】:

copyRange 总是返回 null:

int[] newList = null;
return newList;

IndexOf & lastIndexOf:

阅读IndexOutOfBoundsException 相应地返回正确的值

考虑一下并进行这些修复,我们不能喂每个答案,也可能存在我没有解决的其他错误。

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2021-12-16
    • 2019-10-05
    • 1970-01-01
    相关资源
    最近更新 更多