Computer Science A
Course Progress
0/0
OCS Build and Lesson
Code Runner - Java
Code Runner - Examples
Code Runner - JavaScript
FRQ - Methods and Control Structures
Challenge Submission Test
2021 FRQ 3
2023 FRQ 3
2024 FRQ 3
2024 FRQ 2
2024 FRQ 1
2024 FRQ 4
FRQ 2 - Sign Class
2023 FRQ 1
2021 FRQ 2
2019 FRQ 4
2019 FRQ 2
2019 FRQ 1
2016 FRQ 3
2018 FRQ Question 4
2018 FRQ Question 3
2018 FRQ Question 2
2018 FRQ Question 1
2017 FRQ 4
2017 FRQ 3
2017 FRQ Question 2
2017 FRQ 1
2016 FRQ 4
2016 FRQ 2
2016 FRQ Q1
FRQ - 2D Arrays
FRQ - ArrayLists
2025 FRQ 4
2025 FRQ 3
2025 FRQ 2
2025 FRQ 1
FRQ - Classes
FRQ - Array
2023 FRQ 4
2022 FRQ 4
2022 FRQ 3
2022 FRQ 2
2022 FRQ 1
2021 FRQ 4
2021 FRQ 1
2015 FRQ 4
2015 FRQ 2
2015 FRQ 1
2015 FRQ 3
2014 FRQ Q2 - Writing a Class
2019 FRQ 3
2014 FRQ 1
Sprint View
Week 19
FRQ - Array
FRQ - Array
1 min read
Array
- fixed sized
- single or multidimensional
1. Declare
int[] arr1;
int[] arr2;
2. Initialize
//separating declare and initialize
arr1 = new int[5];
arr2 = new int[]{1,2,3};
// declare and initialize at the same time
int[] arr1 = new int[5];
int[] arr2 = {1,2,3}; // an array literal
3. Access element using []
arr2[0] // accesses the first element in the array
Arraylist
- resizable
- uses methods to access and modify elements
- typically stores objects
How to Create Arraylists:
- Declare and Initialize
ArrayList<String> al = new ArrayList<String>();
- Basic Operations
Insertion using add()
al.add("apple");
al.add("orange");
al.add("banana");
Access using get()
al.get(0);
Deletion using remove()
al.remove("orange");
Update using set()
al.set(0, "kiwi");
Warmup
Write a java program to sort an Arraylist
Checklist to Maximize Points ✅
Before Writing Code
- Understand the method signature (what the methods do, the return types, access modifier)
- Paying attention to input type (e.g. array vs ArrayList)
Writing Code
- Use loops carefully (consider bounds)
- Check for null/empty cases
Before Submitting Code
- Correct return type
- Check whether syntax is used for array/ArrayList
Common Mistakes to Avoid ❌
[] vs get Confusion (penalty)
[]: used to access elements in array
get: used to access elements in ArrayList
int[] arr = {1,2,3};
System.out.println(arr[0]);
ArrayList<String> al = new ArrayList<String>();
al.add("sprite");
System.out.println(al.get(0));
.length vs .size() Confusion (no penalty)
.length: property for length of a array .size(): method for length of an Arraylist
String[] colors;
colors = new String[]{"yellow", "purple", "blue"};
System.out.println(colors.length);
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(12);
nums.add(10);
System.out.println(nums.size());
Traversing Arrays/ArrayLists
- Ensure bounds are correct (applying the right comparison/logic)
- Account for dynamic resizing of ArrayLists for
.add()and.remove()
// starts at the last index (.size() - 1)
// ends at the first index
for (int i = temps.size() - 1; temps >= 0; i--) {
// insert code
}