Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 6, Size: 6 [duplicate]

Since the Size of the list is 6, so indexes are 0,1,2,3,4,5 (which is value of i in this case)

You are always increasing the value of i by 1 and when it is i = 5, it is throwing IndexOutOfBoundsException.

As a fix you can do

if (i < 5 && current.equals(listaTags.get(i + 1))) {...}

Leave a Comment