Unit 6 gave you the knowledge to work with collections of related data. While arrays are useful and convenient, they do have their limitations.
Size: The size of the array must be established at the time of creation and cannot be changed. You would need to rebuild a brand-new array if you wanted a bigger one! Do you always know how big your data will be? Adding/removing: It is not possible to actually remove elements in an array other than re-assigning it some invalid value! You also cannot add to an array so that it includes one or two more elements from its original size. This goes back to the fact that the size must be known during creation. Collections of data that may change can be things like a shopping list or a to-do list. They are a collection of data where you could expect someone to want to add or remove elements from it. ArrayList is the answer to this limitation! The underlying structure is actually an array that grows and shrinks, but that is automatically done for the user of the data structure. You do not need to define a starting size for an array and you can add and remove freely. You may see other CSA material refer to ArrayList as a List. This is actually because ArrayList is the actual implementation of a List but they do represent the same thing. A List is an interface describing a set of behavior for what it means to be a list while an ArrayList is the actual implementation of the behaviors! Interfaces are no longer on the AP Exam. |
Creating ArrayListsThe line below is how you declare and create an ArrayList. Similar to an array, you do not have the actual data structure until you call the constructor for the list.
ArrayList
|
List Methods The following are all of the methods you should be familiar with to be prepared for the AP Exam.
int size() returns the number of elements in the list boolean add(E obj) appends obj to the end of the list and returns true E remove(int index) removes the item at the index and shifts remaining items to the left (to a lower index) void add(int index, E obj) moves any current objects at index or beyond to the right (to a higher index) and inserts obj at the index E get(int index) returns the item in the list at the index E set(int index, E obj) replaces the item at index with obj size vs length Arrays use .length while lists uses size(). You can think of lists using a getter to obtain the size while arrays use a property/field. |
|
|
|
List TraversalTraversing lists look similar to traversing arrays as shown below. The loop is still mainly responsible for generating the addresses (indexes) to the elements. It is up to the programmer what to do once they have access to the element.
int[] vals = {1 , 5, 2, 5}; int sum = 0; for(int i =0; i < vals.length; i++){ sum += vals[i]; } ArrayList |
Must Know OperationsYou should familiarize yourself with these common algorithms for lists. You will be in a good place if you can perform these on lists and arrays.
|
Linear SearchIn AP CSA, if you have unsorted data, a linear/sequential search is the only method that can be used to find a value. It will start at the beginning and walk through the entire array until it finds the element or doesn't find the element. It will return the index of the element if found otherwise it will return -1. Below are two versions of linear search using an array and a list.
public static int sequentialSearch(String[] elements, String target){ for (int j = 0; j < elements.length; j++){ if (elements[j].equals(target)){ return j; } } return -1; } public static int sequentialSearch(int[] elements, int target){ for (int j = 0; j < elements.length; j++){ if (elements[j] == target){ return j; } } return -1; } |
Binary SearchWhen you have sorted data, you can use Binary Search.
Binary search will divide the sorted search space into halves. It compares a target value to the value in the middle of a range of indices. If the value isn't found it looks again in either the left or right half of the current range. Each time through the loop it eliminates half the values in the search area until either the value is found or there is no more data to look at. Below is an animation showing searching for a negative number that does not exist in the data. Included is also the code used for binary search without recursion. How would you change it to work with a list instead of an array?
public static int binarySearch(int[] elements, int target) { int left = 0; int right = elements.length - 1; while (left <= right){ int middle = (left + right) / 2; if (target < elements[middle]){ right = middle - 1; } else if (target > elements[middle]){ left = middle + 1; }else { |
You will learn about 3 sorting algorithms that will be on the AP Exam in some fashion. Typically they show up in multiple-choice questions. Insertion Sort - Insert the next unsorted element in the already sorted part of the array by moving larger values to the right. Start at index 1 and loop through the entire array. |
Selection SortThe selection sort that you need to know for the exam starts at index 0 and looks through the entire array keeping track of the index of the smallest value in the array and then swaps the value at the smallest index with the value at index 0. Then it does the same thing for index 1, then 2, and so on until it reaches the length of the array minus one. At this point, the array is sorted in ascending order.
public static void selectionSort(int[] elements){ for (int j = 0; j < elements.length - 1; j++){ int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]){ minIndex = k; } } int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; } } |
Insertion SortThe insertion sort that you need to know for the exam starts at index 1 and inserts the value at index 1 into its correct place in the already sorted part (the part to the left of the current index). It moves any value larger than the value stored in temp to the right until it either finds the appropriate place to put temp or gets to the front of the array.
public static void insertionSort(int[] elements){ for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]){ elements[possibleIndex] = elements[possibleIndex - 1]; possibleIndex--; } elements[possibleIndex] = temp; } } |