繁体   English   中英

是for循环导致此代码错误的原因?

[英]Is the for loop what is causing the error in this code?

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

那么我应该先修复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;        
    }
}

我应该将其作为结果,但事实并非如此,我一直在这部分出现ERROR意外。 谢谢。

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

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

获得indexOf(5)的值:[] OK-预期indexOf返回-1并得到:-1

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

得到indexOf(20)的值:[20] OK-预期indexOf返回0并得到:0

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

得到的lastIndexOf(20)为:[20] OK-预期lastIndexOf返回0并得到:0

正在获取lastIndexOf(25),该结果为:[20]错误-预期lastIndexOf返回-1,但得到:0

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

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

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

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

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

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

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

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

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

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

完成-按Enter键结束程序

让我们从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。

问题是您初始化了last

以一个StringSAMPLE "SAMPLE".toCharArray().length将为5,但是在循环中,您希望从"SAMPLE".toCharArray()[4]循环到"SAMPLE".toCharArray()[0]

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM