简体   繁体   中英

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". If the array parameter is length 0, it must return -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. It doesn't read or print anything.

So should I fix how the intvalue=0 at first? Or is my for loop faulty? Or in the second loop, should I just not have int last as 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. 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 ---

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

Getting lastIndexOf of a null array ERROR - lastIndexOf threw an unexpected exception: java.lang.NullPointerException

Getting indexOf(5) of: [] OK - expected indexOf to return -1 and got: -1

Getting lastIndexOf(5) of: [] OK - expected lastIndexOf to return -1 and got: -1

Getting indexOf(20) of: [20] OK - expected indexOf to return 0 and got: 0

Getting indexOf(25) of: [20] ERROR - expected indexOf to return -1 but got: 0

Getting lastIndexOf(20) of: [20] OK - expected lastIndexOf to return 0 and got: 0

Getting lastIndexOf(25) of: [20] ERROR - expected lastIndexOf to return -1 but got: 0

Getting indexOf(5) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 0 but got: 6

Getting indexOf(10) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 1 but got: 7

Getting indexOf(15) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 2 but got: 8

Getting indexOf(20) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 3 but got: 9

Getting indexOf(0) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return -1 but got: 0

Getting lastIndexOf(5) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 6 but got: 0

Getting lastIndexOf(10) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 7 but got: 1

Getting lastIndexOf(15) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 8 but got: 2

Getting lastIndexOf(20) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 9 but got: 3

Getting lastIndexOf(0) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return -1 but got: 0

Done - press enter key to end program

Let's start with 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:

  • Test list[0] == 5 . True, so indexValue = 0 .

Ideally, we should return 0 at this point. But that's not what happens:

  • Test list[1] == 5 . False, so do nothing.
  • Test list[2] == 5 . True, so indexValue = 2 .
  • Test list[3] == 5 . False, so do nothing.
  • Test list[4] == 5 . True, so indexValue = 4 .
  • Return 4 . Not 0 , as it should.

As it turns out, indexOf is working like lastIndexOf should. 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 . Let's say we have a 1-element array, like this:

[1]

The last index of this array is 0 , but you are initializing last with list.length , which is 1 , not 0 . You have to subtract 1.

The problem is of your initialization of last .

Taking a String of SAMPLE . 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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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