简体   繁体   中英

While-Loop not checking the full conditional (first part is skipped)

I'm fairly new with Java and am attempting to write a method that will return the starting index of the longest run of numbers in an array. (if the array is {2,3,5,5,5,2,2} then '2' should be returned as it is the start of the longest string of identical numbers).

In my code I have a while-loop which should continue running as long as the as the next number in the array is equal to the current value. (will run until a new number appears).

while((i < values.length) && (values[i] == values[i+1]))
{
   currentRun++;
   i++;          
} 

This can lead to an index out of bounds error if a run occurs at the end of an array. {3,7...5,5,5} because it will continue looping and eventually try to check the value of an index that does not exist. In order to keep this from occurring I tried to add to the conditional and make sure that the index value has to be less then the length of the array but when I use a java visualizer it always continues running the for loop regardless of the first part of the conditional.

Can anyone please explain why this is occurring and how to fix it?

(full program code below)

public class NumberCube
{

    public static int getLongestRun(int[] values)
    {
      
       int longestIndex = -1;
       int currentIndex = 0;
       
       int longestRun = 1;
       int currentRun = 0;
       
       for(int i = 0; i < values.length - 1; i++)
       {
         
         if(values[i] == values[i+1])
         {
           currentIndex = i;
           
           while((i < values.length) && (values[i] == values[i+1]))
           {
             currentRun++;
             i++;          
           } 
         }
           
         if (currentRun > longestRun)
         {
           longestRun = currentRun; 
           longestIndex = currentIndex;
         }
         
         currentRun = 1;
   
       }//end for loop
       return longestIndex;
    }//end method

    public static void main(String[] args){
        int[] values = {3, 5, 6, 6, 3, 6, 4, 4, 4, 2, 6, 4, 1, 1, 1, 1};
        int longestRunIdx = getLongestRun(values);

        if(longestRunIdx != 12){
           System.out.println("Your code does not return the correct index.");

           if(longestRunIdx == 2 || longestRunIdx == 6)
               System.out.println("It is returning the start index of a run, but that run is not the longest.");

           System.out.println("Remember that your code must return the start index of the longest run of tosses.");
        } else {
           System.out.println("Looks like your code works well!");
        }
    }
}

It does check, the query doesn't prevent the invalid access because the array is zero-based. So, eg if array.length == 1 , then array[0] is valid, but array[1] isn't.

For i == 0 , you access both, hence the invalid index for i+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