Reputation: 975
From my AP Computer Science A review book:
Consider this program segment. You may assume that the
wordList
has been declared asArrayList<String>
.for(String s : wordList) if(s.length() < 4) System.out.println("SHORT WORD");
"What is the maximum number of times SHORT WORD can be printed?"
The book says the answer is wordList.size(), but why wouldn't it be wordList.size()-1? Is the index of an ArrayList different from a regular array? My book says something about it automatically adding one to the index but I may have read that wrong.
Upvotes: 1
Views: 2220
Reputation: 1344
Infinetely many times. word list is declared as ArrayList, but it can actually be subclass of array list. Thus you can make array list that returns infinite iterator returning empty strings.
ArrayList<String> wordList = new ArrayList<String>() {
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public String next() {
return "";
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
};
}
};
for (String s : wordList) {
if (s.length() < 4) {
System.out.println("SHORT WORD");
}
}
Upvotes: 1
Reputation: 95508
The book says the answer is wordList.size(), but why wouldn't it be wordList.size()-1?
It won't and it can't, because the maximum number of times it can be printed, is if all words in wordList
have lesser than four characters.
I think you might be confusing the number of elements in the ArrayList
with the valid indices of an ArrayList
. Arrays and List
s in Java are zero-based and so run from 0
to list.size() - 1
. This means the last element is at list.size() - 1
. Think of it this way. If you have the numbers 0, 1, 2, 3
, how many numbers do you have in total? You have four numbers. However if these numbers were shoved into a zero-based array then the first element is at location 0
, the second element is at location 1
and so on until you have the last element at location 3
.
This picture may help you out:
Indices: 0 1 2 3
+---+---+---+---+
Values: | 1 | 2 | 3 | 4 |
+---+---+---+---+
Upvotes: 6
Reputation: 135752
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.
Upvotes: 1
Reputation: 4121
for(String s : wordList)
if(s.length() < 4)
System.out.println("SHORT WORD");
basically says "Call the first word in the list s. If this word is less than 4 letters long, print "SHORT WORD" to the console. Now, repeat for the second word and so on until there are no words left."
Therefore, if all of the words in the list were less than 4 letters long, "SHORT WORD" would be printed wordlist.size() times.
Upvotes: 1
Reputation: 26574
The maximum index of an array or ArrayList will be size-1. This is because the index of a collection is 0 based. You can think of the index as an offset.
Let's say I have an ArrayList with the following contents:
["abc"]
There is 1
element in the array, so its size is 1
. The index (or offset) of the first element is 0
because it is at the root of the array. If my array is:
["abc", "bcd", "cde", "def" ]
Then you can see that there are 4 entries in the array, so the size is 4
. The offset to the final element "def" is 3 because you have to take 3 steps forward from the root to get there. 0 steps gives you "abc", 1 step gives "bcd", 2 = "cde" and 3 = "def". So the size
is 4 but you can't get the item at the 4th index because 4 steps takes you beyond "def".
Upvotes: 1
Reputation: 2308
Of course the maximum is wordList.size(), this happens when all wordList's words are short words. Then, wordList from position 0 to position wordList.size() - 1 (both included) are short.
Upvotes: 2