WordPairList is #2 from the from the 2018 AP Computer Science A Free Response problems.


Part (a) WordPairList constructor

public WordPairList(String[] words)
    allPairs = new ArrayList<WordPair>();

    for(int i = 0; i < words.length; i++)
        for(int j = i + 1; j < words.length; j++)
            allPairs.add(new WordPair(words[i], words[j]));

It is very common for students to miss the first line of this constructor. Adding to a list (or setting elements in an array) is not the same thing as initializing the variable.

Part (b) numMaches method

public int numMatches()
    int matches = 0;

    for(WordPair wp : allPairs)

    return matches;

2018 AP CS Exam Free Response Solutions

Additional ArrayList resources

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on WordPairList