You ask:
"What is the maximum number of times SHORT WORD can be printed?"
If all items of wordList
have length < 4
, then all items will be printed.
So, how many itens does wordList
have? Simple: wordList.size()
.
ArrayList
's size()
method semantic:
In a ArrayList
with a total of 3
itens, such as:
ArrayList wordList = new ArrayList<String>();
wordList.add("firstWord");
wordList.add("2ndWrd");
wordList.add("3w");
The size()
is the number of items (like array.length
). So, in this case, 3
.
But ArrayList
's index, as a regular array, is 0-based (going from 0
to size()-1
). So:
wordList.get(0); // returns "firstWord"
wordList.get(1); // returns "2ndWrd"
wordList.get(2); // returns "3w"
wordList.get(3); // throws an exception (IndexOutOfBoundsException)
Thus your confusion.