1

From my AP Computer Science A review book:

Consider this program segment. You may assume that the wordList has been declared as ArrayList<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.

11
  • 3
    I'm confused, what is the question, really? Commented May 6, 2013 at 21:24
  • 5
    "the answer is wordList.size()" What's the question? Commented May 6, 2013 at 21:24
  • 1
    I'm assuming this is java. ArrayList is zero-based (as are arrays). No difference there. Commented May 6, 2013 at 21:26
  • 2
    To provide an answer to this question, one must first provide the question. How zen. Commented May 6, 2013 at 21:30
  • 2
    @Mia Ccio: Don't worry. The question does make a lot more sense in it's updated version. Success with your exam! Commented May 6, 2013 at 21:32

6 Answers 6

6

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 Lists 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 |
         +---+---+---+---+
Sign up to request clarification or add additional context in comments.

1 Comment

@MiaCcio No problem. Good luck on your exam!
2

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.

3 Comments

Sorry, I'm still confused, if all the words are short, then why would it still be wordList.size()? Wouldn't it be one less than that?
OH WAIT I'm referring to the indices rather than the actual number of times, aren't I?
The number of String elements that you have in wordList is wordList.size(). The first position that contains an String in wordList is 0 and the last one is wordList.size() - 1. So, yes you are confusing with the maximum index of the data structure which is wordList.size() - 1. But you should look at the total number of elements which is wordList.size().
1

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".

Comments

1
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.

Comments

1

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.

1 Comment

I think I got messed up on the actual indices rather than what the question really asked. I put the last index to be processed, not the number of times. Thank you!
1

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");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.