Pattern 1
Constructor That Builds an ArrayList From Data
Part A often asks you to write a constructor that populates an instance variable ArrayList from parameter data.
public class Tournament
{
private ArrayList players;
public Tournament(String[] names, int[] ratings)
{
players = new ArrayList();
for (int i = 0; i < names.length; i++)
{
players.add(new Player(names[i], ratings[i]));
}
}
}
Grader Checklist
+1 Initialize players as new ArrayList (not redeclare as local). +1 Correct loop over parameter arrays. +1 Create new object with correct constructor args. +1 Add each object to the list.
Pattern 2
Filter and Remove From ArrayList
Part B often asks you to remove elements that meet a condition.
public void removeInactive()
{
for (int i = players.size() - 1; i >= 0; i--)
{
if (!players.get(i).isActive())
{
players.remove(i);
}
}
}
Grader Checklist
+1 Correct backward traversal (or forward with index adjustment). +1 Correct condition check using accessor method. +1 Correct removal. +1 All matching elements removed (not just first). +1 No ConcurrentModificationException.
Pattern 3
Build Pairs or Groups From a List
A common Part B pattern: pair elements from both ends of the list, or group consecutive elements.
public ArrayListbuildMatches() { ArrayList matches = new ArrayList (); int left = 0; int right = players.size() - 1; while (left < right) { matches.add(new Match(players.get(left), players.get(right))); left++; right--; } return matches; }
Common Penalty Triggers
ArrayList players = new ArrayList(); inside the constructor creates a LOCAL variable that shadows the instance field. The instance field stays null. This costs 1 point every time.Get FRQ-Ready Before May 15
54.5% of my students score 5s. The Cram Kit includes daily FRQ practice with scoring breakdowns.
Get the Cram Kit — $29.99 1-on-1 Tutoring