我的代码有什么问题,我一直收到错误?

时间:2014-07-17 05:24:12

标签: java arrays computer-science

我以为我做得对,但显然没有。 当我用一个演示类运行整个代码时,这部分给了我一些麻烦和一些错误。我想知道为什么这行,抛出新的ArrayIndexOutOfBoundsException(startIndex);是一个问题。 请帮忙。 在底部是我不断得到的结果。我不想尝试回答,但是我花了两天时间尝试调试,但我对这部分的错误感到困惑。

   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;
                }

这是我得到的结果

---测试copyRange方法---

Getting copyRange of a null array
  OK - copyRange threw exception for null array: BadArrayException

Getting copyRange(list,-1) of: []
  OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

Getting copyRange(list,0) of: []
  ERROR - expected copyRange to return an array of length 0 but got a null array

Getting copyRange(list,1) of: []
  OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1

Getting copyRange(list,-1) of: [20]
  OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

Getting copyRange(list,0) of: [20]
  ERROR - expected copyRange to return an array of length 1 but got a null array

Getting copyRange(list,1) of: [20]
  ERROR - expected copyRange to return an array of length 0 but got a null array

Getting copyRange(list,2) of: [20]
  OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2

Getting copyRange(list,-1) of: [10,15]
  OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

Getting copyRange(list,0) of: [10,15]
  ERROR - expected copyRange to return an array of length 2 but got a null array

Getting copyRange(list,1) of: [10,15]
  ERROR - expected copyRange to return an array of length 1 but got a null array

Getting copyRange(list,2) of: [10,15]
  ERROR - expected copyRange to return an array of length 0 but got a null array

Getting copyRange(list,3) of: [10,15]
  OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3

Getting copyRange(list,-1) of: [30,35,40,45,50]
  OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

Getting copyRange(list,0) of: [30,35,40,45,50]
  ERROR - expected copyRange to return an array of length 5 but got a null array

Getting copyRange(list,1) of: [30,35,40,45,50]
  ERROR - expected copyRange to return an array of length 4 but got a null array

Getting copyRange(list,2) of: [30,35,40,45,50]
  ERROR - expected copyRange to return an array of length 3 but got a null array

Getting copyRange(list,3) of: [30,35,40,45,50]
  ERROR - expected copyRange to return an array of length 2 but got a null array

Getting copyRange(list,4) of: [30,35,40,45,50]
  ERROR - expected copyRange to return an array of length 1 but got a null array

Getting copyRange(list,5) of: [30,35,40,45,50]
  ERROR - expected copyRange to return an array of length 0 but got a null array

Getting copyRange(list,6) of: [30,35,40,45,50]
  OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 6

1 个答案:

答案 0 :(得分:1)

在第二个else分支的末尾,您需要返回newList数组。您正确复制元素,但您永远不会返回它。在for周期后立即执行此操作:

// ...
for (i = startIndex; i < list.length; i++)
    if (startIndex == 0){
        newList[i] = list[i];
    }else{
        newList[x] = list[i];
        x += 1;
    }
// THIS IS MISSING:
return newList;

注意:添加此return语句后,您当前的最后两行将变得不必要,因为在if语句的所有分支中,执行将返回或抛出异常。事实上,如果你不删除它们,它们会给你"Unreachabe code"错误。