【发布时间】: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