FRQ Type 3: Array and ArrayList
array and arraylist
Part of: Exam Workshop: FRQ Strategies
What This FRQ Asks. The array FRQ gives you a one-dimensional array or an ArrayList and asks you to search it, sum it, find a maximum, or build a new list from it. The rubric rewards a correct traversal and correct index or element handling. Off-by-one errors are the number one point loss here, so bound your loops carefully. Traversing an Array. When you only read elements, a for-each loop is clean and avoids index mistakes: Start max at arr[0], not at 0, or an array of all negative numbers would give the wrong answer. When you need the index, use a counted loop with the correct bound i < arr.length: ArrayList Basics. An ArrayList uses methods instead of brackets. The four you need are size(), get(i), add(x), and remove(i). When you remove from an ArrayList inside a loop, be careful. After remove(i), every later element shifts left by one, so blindly doing i++ skips the next element. A common fix is to not increment i in the iteration where you removed. What usually goes wrong. What you see What caused it How to fix it --- --- --- ArrayIndexOutOfBoundsException on the final pass The loop bound used <= against the length Valid indices stop at length - 1, so bound the loop with < An
Challenge: Find the Maximum