WordList is #1 from the from the 2004 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/apc/ap04_frq_compsci_a_35988.pdf

On the 2004 AP CS A Exam, the ArrayList get method returned a reference of type Object. It was necessary to cast the reference to the correct type to call most methods. If this question appeared on a more recent AP CS A Exam, myList would have been declared as ArrayList<String> myList and casting would not have been required.

The 2004 AP CS A Exam did not feature enhanced for loops.

Part (a) numWordsOfLength method

public int numWordsOfLength(int length)
{
    int numWords = 0;

    for(int i = 0; i < myList.size(); i++)
    {
        String word = (String) myList.get(i);
        
        if(word.length() == length)
            numWords++;
    }

    return numWords;
}

Part (b) removeWordsOfLength method

public void removeWordsOfLength(int length)
{
    int i = 0;

    while(i < myList.size())
    {
        String word = (String) myList.get(i);
        
        if(word.length() == length)
            myList.remove(i);
        else
            i++;
    }
}

See ArrayList practice for details on adding to and removing from an ArrayList within a loop.

2004 AP CS A Exam Free Response Solutions

Additional ArrayList resources

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on WordList