0% found this document useful (0 votes)
2 views8 pages

? AP CSA ArrayList

This document provides a quick reference guide for AP Computer Science A, focusing on ArrayLists and loops. It covers basic ArrayList operations, common patterns for counting, summing, finding maximum values, filtering, and modifying elements, along with important rules and tricks for handling ArrayLists effectively. Key strategies for exam preparation and common question types are also highlighted to aid students in their understanding and application of these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

? AP CSA ArrayList

This document provides a quick reference guide for AP Computer Science A, focusing on ArrayLists and loops. It covers basic ArrayList operations, common patterns for counting, summing, finding maximum values, filtering, and modifying elements, along with important rules and tricks for handling ArrayLists effectively. Key strategies for exam preparation and common question types are also highlighted to aid students in their understanding and application of these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🧠 AP CSA — FRQ QUICK SHEET (LOOPS + ARRAYLISTS)

🔹 1. ARRAYLIST BASICS

Declare

ArrayList<Integer> nums = new ArrayList<Integer>();


ArrayList<String> names = new ArrayList<String>();

Add values

[Link](5);
[Link]("Alex");

Get value

[Link](i);

Size

[Link]()

🔹 2. LOOP OVER ARRAYLIST (MOST IMPORTANT)

for(int i = 0; i < [Link](); i++)


{
[Link](i);
}

👉 ALWAYS use:

 i < [Link]()

 [Link](i)

🔹 3. COUNT PATTERN (VERY COMMON FRQ)

int count = 0;

for(int i = 0; i < [Link](); i++)


{
if(condition)
{
count++;
}
}

👉 Keywords:

 “how many”

 “count”

🔹 4. SUM PATTERN

int sum = 0;

for(int i = 0; i < [Link](); i++)


{
sum += [Link](i);
}

🔹 5. MAX PATTERN

Normal max:

int max = [Link](0);

for(int i = 1; i < [Link](); i++)


{
if([Link](i) > max)
{
max = [Link](i);
}
}

Max with condition (EVEN / FILTERED):

int max = Integer.MIN_VALUE;

for(int i = 0; i < [Link](); i++)


{
if([Link](i) % 2 == 0)
{
if([Link](i) > max)
{
max = [Link](i);
}
}
}

🔹 6. FILTER (CREATE NEW LIST)

ArrayList<Integer> result = new ArrayList<Integer>();

for(int i = 0; i < [Link](); i++)


{
if([Link](i) > 5)
{
[Link]([Link](i));
}
}

🔹 7. KEY AP RULES (VERY IMPORTANT)

Arrays vs ArrayLists

 Array → [Link], arr[i]

 ArrayList → [Link](), [Link](i)

Loop direction

 forward: i++

 never use i-- unless looping backwards

Off-by-one rule

i < [Link]() // correct


i <= [Link]() // WRONG
🔹 8. WHEN YOU FREEZE DURING FRQ

Do THIS:

1. write loop first

2. write if statement

3. write update line

Even if incomplete → you still get points

EXAM PREPARATION

🧠 AP CSA ARRAYLIST “TRICKY FRQ” SHEET

🔹 1. Core rules (NEVER forget)

[Link](i)
[Link]()
[Link](value)
[Link](i)

🔥 2. REMOVE ELEMENTS (VERY IMPORTANT TRICK)

❌ WRONG way (causes skipping)

for(int i = 0; i < [Link](); i++)


{
if([Link](i) < 5)
{
[Link](i);
}
}

👉 Problem: shifting indexes → skips elements

✅ SAFE way (REVERSE LOOP)

for(int i = [Link]() - 1; i >= 0; i--)


{
if([Link](i) < 5)
{
[Link](i);
}
}

🧠 WHY reverse works

Because:

 you remove from the back

 indexes don’t shift forward incorrectly

🔥 3. COUNT + FILTER (COMMON TRICK)

Count AND build new list

ArrayList<Integer> result = new ArrayList<Integer>();

for(int i = 0; i < [Link](); i++)


{
if([Link](i) % 2 == 0)
{
[Link]([Link](i));
}
}

👉 Pattern = “filtering”

🔥 4. MODIFY IN PLACE (CHANGE VALUES)

for(int i = 0; i < [Link](); i++)


{
if([Link](i) < 0)
{
[Link](i, 0);
}
}

👉 set(i, value) = replace element


🔥 5. FIND AND STOP EARLY (HARDER FRQ TYPE)

int i = 0;

while(i < [Link]() && [Link](i) != target)


{
i++;
}

👉 stops when found OR reaches end

🔥 6. COMMON TRICK QUESTIONS

“remove all even numbers”

→ reverse loop + remove

“count elements that match condition”

→ count++

“build new filtered list”

→ add to new ArrayList

“replace values”

→ set()

⚠️7. MOST IMPORTANT TRICK CONCEPTS

1. Index shifting (removal problem)

 forward loop = dangerous

 reverse loop = safe

2. Reading vs modifying

 get(i) = read

 set(i, ...) = replace

 add() = expand list


 remove() = shrink list

3. Loop direction matters

 forward → normal processing

 backward → safe removal

🧠 8. EXAM “SURVIVAL PATTERN”

When you see ArrayList FRQ, always ask:

1. Am I filtering? → use add()

2. Am I counting? → use count++

3. Am I changing values? → use set()

4. Am I removing? → use reverse loop

🔥 9. SUPER COMMON AP TRICK QUESTION TYPES

You WILL see:

 remove based on condition

 count matches

 replace values

 build new list from old

 loop + nested if logic

💡 FINAL SIMPLE MEMORY

ARRAYLIST ACTIONS:

 get() → read

 add() → insert

 remove() → delete

 set() → replace

 size() → length

You might also like