-1

just getting into java. What is the best way to encounter this issue? I understand what is happening, and I am asking for the best way to solve it, cause my solution is a bit ugly I might say.

public static int jumpingOnClouds(List<Integer> c) {

        int size = c.size();
        int jumpx = 0;
        
        for(int i = 0; i<size; i++){
            jumpx++;
            if(c.get(i+2)==0)i++; //Last 2 iteration will gives the error.
        }

        return jumpx;
    }

My newbie solution

public static int jumpingOnClouds(List<Integer> c) {

        int size = c.size();
        int jumpx = 0;
        
        for(int i = 0; i<size; i++){
            jumpx++;
            
            if (!(i+1>=size || i+2>=size)){ //adding this to avoid c.get(i+2) to be executed.
                if(c.get(i+2)==0)i++;
            }
            
            if(i+2>=size || i+1>=size)break; //adding this to avoid jumpx++ to be executed.
            
        }
        return jumpx;
    }

I might be a bit blur, but I hope someone can explain and shows the best one (with the code). Thank you.

DoubleLiu
  • 52
  • 1
  • 6
  • 2
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Progman Aug 31 '21 at 19:45
  • 1
    It's not clear what you want this code to do in the first place. Can you describe with words what you want to achieve? – Gaël J Aug 31 '21 at 19:46
  • i don't really think there is anything to do with the objective. the upper code is just the says 'psuedo code'. but due to the limitation of java, the mentioned error will happen at the last iteration, in the get(i+2) where i will be max of size and max i+2 will return error cause it outsize the arraylist. i am just finding the best way to break out of the loop without defining so many amateur conditional if.. – DoubleLiu Aug 31 '21 at 19:57

3 Answers3

1

You can validate the i + 2 value to be inside the range before trying to perform c.get(i+2) to avoid the exception

public static int jumpingOnClouds(List<Integer> c) {

        int size = c.size();
        int jumpx = 0;
        
        for(int i = 0; i<size; i++){
            jumpx++;
            if(i+2 <size && c.get(i+2)==0)i++;
        }

        return jumpx;
    }
Unused hero
  • 307
  • 1
  • 9
  • This is not equivalent to OP's "newbie solution". The for loop will not exit when it is supposed to, hence incrementing `jumpx` more times than it should be – ajc2000 Aug 31 '21 at 19:51
  • Agree with you, but I think this is a minor issue while the added validation makes minimal change with the original code – Unused hero Aug 31 '21 at 19:56
  • Thank you for your help, i learn something. Yet, i still have to put if(i+2 < size || i+1 < size){jumpx++;} just to encounter the extra loop. but it is good, i can define -1 to jumpx. – DoubleLiu Aug 31 '21 at 20:23
  • In case may answer resolved your problem please accept it. While I'm not sure it is the best, I see people with huge score amounts here, they gave other approaches :) – Unused hero Aug 31 '21 at 20:28
0

A simpler way to modify the original code (without encountering an IndexOutOfBoundsException) would be:

public static int jumpingOnClouds(List<Integer> c) {
    int size = c.size();
    int jumpx = 0;
        
    for(int i = 0; (i + 2) < size; i++){
        jumpx++;
        if(c.get(i+2)==0)i++;
    }

    return jumpx;
}

Since you are modifying i, and need to access from the (i + 2)-th index, you need to ensure that (i + 2) < size, since size - 1 is the last valid index in the List

ajc2000
  • 651
  • 5
  • 20
  • Hey thanks for the answer. Unfortunately it doesnt work since I have i++ in the loop, and it will skip the last iteration in some cases. but I learned something new. is there any idea? thanks man – DoubleLiu Aug 31 '21 at 20:26
0
    // here you are looping through all elements in the list
    for(int i = 0; i<size; i++){ 
        jumpx++;
        //here you are getting the "i + 2"th element. 
        //This is going to be out of bounds if i is the 2nd
        //to last or the last element
        if(c.get(i+2)==0)i++;
    }
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80