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