Skip to content
Advertisement

Why my IF can’t get some of my ArrayList’s indexes?

My idea here is to check whether the elements of my ArrayList start with A, B, C, or with another letter, but for some reason, my IF can’t get some words, such as “Almofada”.

JavaScript

Output

JavaScript

Advertisement

Answer

Your issue is with your for loop. You are incrementing the index ‘i’ on each iteration of the loop, then removing the word at words(i). Because i gets incremented, but the size of the words list is reduced each iteration, ‘i’ quickly increases to the new size of the words list without actually evaluating the Strings at the end.

To correct, you simply need to avoid incrementing i on each iteration:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement