-1

I have two arraylists - retrievedList with 15 elements and originalList with 3 elements. i.e. one is the subset of another. I am comparing these two arraylists to check if the 3 elements of Original list appear in the same order in retreiveList as well.

Basically, the method is looping over each index in the retrievedList and comparing with originalList, when a match is found the counter is incremented. If the counter size is same as the originalList size, the test passes else fails.

Now, the error i get is java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

I am not sure why the method is retreiving index (3) and how to stop this!

I did a sysOut of counter, which gives the output 0 0 0 0 0 0 0 1 2 3 This tells me that (i) the counter has been incremented to 3. (ii) it looped 10 times, while it should have looped 15 times.

I tried to end the for loop after the if-else statement.. it dint work.. was looping over just once.

I tried to put a break in the if statement from trying to prevent it to go index(3), didnt work.

instead of i=0, i tried int = -1. dint work.

I cant figure out the problem, please help.

try{
    int counter = 0;
    for(int i=0; i<retrievedList.size(); i++, ++counter){
        if(retrievedList.get(i).equals(originalList.get(counter))){
            System.out.println(counter);
        }
    } //for loop closed
    if(counter==originalList.size()){
         Assert.assertTrue(true, "Arrays are in Order" - Test Passed!)
    }else{  
         Assert.assertTrue(false, "Arrays are not in Order" - Test failed!)
    }
} catch (Exception e){
    logger.error("test for components has failed");
    e.printStackTrace();
}

I do not have an "=" in for loop

  • Possible duplicate of [How to prevent ArrayIndexOutOfBoundsException in Java?](https://stackoverflow.com/questions/5554734/how-to-prevent-arrayindexoutofboundsexception-in-java) – MC Emperor Jan 26 '19 at 13:43
  • I dont believe its a duplicate or possible solution for my answer, because the cause of exception in other solution was a faulty "=" in the for loop. In my case, i dont have that issue in for loop yet i got the same exception. so, that link doesnt help me to solve my problem. – automationLearner Feb 07 '19 at 10:28

4 Answers4

1

add this line in the for loop.

for(int i=0; i<retrievedList.size(); i++){
    if(retrievedList.get(i)==(originalList.get(counter))){
        counter++;
        if(counter == originalList.size())
            break;
        System.out.println(counter);
    }
}
awadhesh14
  • 89
  • 7
0

After counter==2, statement in the loop at the end ie counter++ will make counter =3 and then when this

for(int i=0; i<retrievedList.size(); i++){
    if(retrievedList.get(i).equals(originalList.get(counter))){
    counter++;
    System.out.println(counter);
  }

will again execute that time counter will be 3 so originalList.get(counter) will give arrayIndexOutOfBound exception,since originalList is of size 3 only,but you are asking for 4th index element.

Ratish Bansal
  • 1,982
  • 1
  • 10
  • 19
0

If you need just the first occurrence than you could add limit:

for(int i=0; i<retrievedList.size(); i++){
    if(retrievedList.get(i).equals(originalList.get(counter)) && counter < 3){
    counter++;
    System.out.println(counter);
  } 

But the idea is that you need to test different scenarios. When you target list contains for example 1,2,3 and the input contains 1, 2 , 4, 1, 2, 3 and so on. For that purpose you need to add checks.
By the way you could use: int index=Collections.indexOfSubList(list , sublist); from Collections.indexOfSubList and check the implementation there.

Echoinacup
  • 482
  • 4
  • 12
  • Thank you..ill add checks and see... by the way int index = Collections.indexOfSubList(list2, list) will check for the sublist in the given order and return the first matching index. I have some other elements interspersed, so i believe it wont work. example, If i want to point out { A, B, C, D } contains ordered list { A, D }, indexOfSublist will give me false. – automationLearner Jan 28 '19 at 10:31
  • In this case yes, because it is not a sub sequence. – Echoinacup Jan 28 '19 at 10:34
0

In the for loop you iterate through retrievedList and if the 3 items from originalList are found and counter is set to 3, you still continue the iterations.
So the next comparison will be against originalList.get(3) which throws the error.
Add a break statement in the for loop to terminate it
when all items are found, meaning counter = 3:

    for(int i=0; i<retrievedList.size(); i++){
        if(retrievedList.get(i).equals(originalList.get(counter))){
            counter++;
            System.out.println(counter);
        }
        if(counter==originalList.size())
            break;   
    }
forpas
  • 160,666
  • 10
  • 38
  • 76