简体   繁体   English

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

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

So for this part of my code, I was wondering why my test class was not working. 因此对于代码的这一部分,我想知道为什么我的测试类无法正常工作。 I followed the instructions exactly, but I'm not sure why my I'm getting errors. 我完全按照说明进行操作,但不确定为什么我会出错。 Basically for both parts, If the array parameter is null, it must throw a BadArrayException with the message "Array is null". 基本上,对于这两个部分,如果array参数为null,则必须抛出BadArrayException并显示消息“ Array is null”。 If the array parameter is length 0, it must return -1. 如果数组参数的长度为0,则必须返回-1。 It must not alter the array parameter contents, nor copy the entire array parameter contents to another array. 它不得更改数组参数的内容,也不得将整个数组参数的内容复制到另一个数组。 To find the first occurrence, it must search through the array parameter once. 要找到第一个匹配项,它必须搜索一次array参数。 It doesn't read or print anything. 它不会读取或打印任何内容。

So should I fix how the intvalue=0 at first? 那么我应该先修复intvalue = 0的方式吗? Or is my for loop faulty? 还是我的for循环有问题? Or in the second loop, should I just not have int last as list.length? 还是在第二个循环中,我是否应该只将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;        
    }
}

I am supposed to be getting this as my result but I am not, I keep getting ERROR got unexpected on this portion. 我应该将其作为结果,但事实并非如此,我一直在这部分出现ERROR意外。 Thank you. 谢谢。

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

And here is what I get instead --- Testing indexOf and lastIndexOf method --- 这是我得到的---测试indexOf和lastIndexOf方法-

Getting indexOf of a null array OK - indexOf threw exception for null array: BadArrayException 获取空数组的indexOf确定-indexOf引发了空数组的异常:BadArrayException

Getting lastIndexOf of a null array ERROR - lastIndexOf threw an unexpected exception: java.lang.NullPointerException 获取null数组的lastIndexOf错误-lastIndexOf引发了意外的异常:java.lang.NullPointerException

Getting indexOf(5) of: [] OK - expected indexOf to return -1 and got: -1 获得indexOf(5)的值:[] OK-预期indexOf返回-1并得到:-1

Getting lastIndexOf(5) of: [] OK - expected lastIndexOf to return -1 and got: -1 得到的lastIndexOf(5)为:[]好-预期lastIndexOf返回-1并得到:-1

Getting indexOf(20) of: [20] OK - expected indexOf to return 0 and got: 0 得到indexOf(20)的值:[20] OK-预期indexOf返回0并得到:0

Getting indexOf(25) of: [20] ERROR - expected indexOf to return -1 but got: 0 正在获取indexOf(25)的值:[20]错误-预期的indexOf返回-1但得到:0

Getting lastIndexOf(20) of: [20] OK - expected lastIndexOf to return 0 and got: 0 得到的lastIndexOf(20)为:[20] OK-预期lastIndexOf返回0并得到:0

Getting lastIndexOf(25) of: [20] ERROR - expected lastIndexOf to return -1 but got: 0 正在获取lastIndexOf(25),该结果为:[20]错误-预期lastIndexOf返回-1,但得到:0

Getting indexOf(5) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 0 but got: 6 正在获取indexOf(5)为:[5,10,15,20,10,15,5,10,15,20]错误-预期indexOf返回0,但得到:6

Getting indexOf(10) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 1 but got: 7 正在获取indexOf(10)为:[5,10,15,20,10,15,5,10,15,20]错误-预期indexOf返回1但得到:7

Getting indexOf(15) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 2 but got: 8 正在获取indexOf(15)为:[5,10,15,20,10,15,5,10,15,20]错误-预期indexOf返回2,但得到:8

Getting indexOf(20) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 3 but got: 9 正在获取indexOf(20)为:[5,10,15,20,10,15,5,10,15,20]错误-预期indexOf返回3,但得到:9

Getting indexOf(0) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return -1 but got: 0 正在获取indexOf(0),其值为:[5,10,15,20,10,15,5,10,15,20]错误-预期indexOf返回-1但得到:0

Getting lastIndexOf(5) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 6 but got: 0 正在获取lastIndexOf(5)为:[5,10,15,20,10,15,5,10,15,20]错误-预期lastIndexOf返回6,但得到:0

Getting lastIndexOf(10) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 7 but got: 1 正在获取lastIndexOf(10)为:[5,10,15,20,10,15,5,10,15,20]错误-预期lastIndexOf返回7,但得到:1

Getting lastIndexOf(15) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 8 but got: 2 正在获取lastIndexOf(15),其值为:[5,10,15,20,10,15,5,10,15,20]错误-预期lastIndexOf返回8,但得到:2

Getting lastIndexOf(20) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 9 but got: 3 正在获取lastIndexOf(20)为:[5,10,15,20,10,15,5,10,15,20]错误-预期lastIndexOf返回9,但得到:3

Getting lastIndexOf(0) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return -1 but got: 0 正在获取lastIndexOf(0),其值为:[5,10,15,20,10,15,5,10,15,20]错误-预期lastIndexOf返回-1但得到:0

Done - press enter key to end program 完成-按Enter键结束程序

Let's start with indexOf . 让我们从indexOf开始。

When you find the number you are searching for, you store the index in a variable, and then return the variable when you're done. 找到要搜索的数字后,将索引存储在变量中,然后在完成后返回变量。 Let's see what happens in the [5,10,5,15,5] test: 让我们看看在[5,10,5,15,5]测试中会发生什么:

  • Test list[0] == 5 . 测试list[0] == 5 True, so indexValue = 0 . 是的,所以indexValue = 0

Ideally, we should return 0 at this point. 理想情况下,此时我们应该返回0 But that's not what happens: 但这不是发生的事情:

  • Test list[1] == 5 . 测试list[1] == 5 False, so do nothing. 错误的,什么都不做。
  • Test list[2] == 5 . 测试list[2] == 5 True, so indexValue = 2 . 是的,所以indexValue = 2
  • Test list[3] == 5 . 测试list[3] == 5 False, so do nothing. 错误的,什么都不做。
  • Test list[4] == 5 . 测试list[4] == 5 True, so indexValue = 4 . 是的,所以indexValue = 4
  • Return 4 . 返回4 Not 0 , as it should. 不应该是0

As it turns out, indexOf is working like lastIndexOf should. 事实证明, indexOf的工作方式与lastIndexOf应该的一样。 Instead of returning the index of the last time you find the value, return the index as soon as you find the value. 与其返回上次找到该值的索引,不如返回找到该值的索引。

lastIndexOf has the same problem, but it also has another problem, which is how you initialize last . lastIndexOf有相同的问题,但也有另一个问题,即如何初始化last Let's say we have a 1-element array, like this: 假设我们有一个1元素的数组,如下所示:

[1]

The last index of this array is 0 , but you are initializing last with list.length , which is 1 , not 0 . 该数组的最后一个索引为0 ,但您要使用list.length初始化last ,即1 ,而不是0 You have to subtract 1. 您必须减去1。

The problem is of your initialization of last . 问题是您初始化了last

Taking a String of SAMPLE . 以一个StringSAMPLE The vale of "SAMPLE".toCharArray().length would be 5, but in you loop you want to loop from "SAMPLE".toCharArray()[4] to "SAMPLE".toCharArray()[0] "SAMPLE".toCharArray().length将为5,但是在循环中,您希望从"SAMPLE".toCharArray()[4]循环到"SAMPLE".toCharArray()[0]

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

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

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