Rycal.
← All AP courses
AP Computer Science A · Cram sheet

Unit 4 · Data Collections

30–40% of the AP exam 76 key terms

● Core concept  ·  ○ Supporting concept

4.1 Ethical and Social Issues Around Data Collection

Privacy risk of data collection ● (core concept) — When using a computer, personal privacy is at risk. When developing new programs, programmers should attempt to safeguard the personal privacy of the user.

Algorithmic bias ● (core concept) — Algorithmic bias describes systemic and repeated errors in a program that create unfair outcomes for a specific group of users.

Data set collection method and bias ● (core concept) — Programmers should be aware of the data set collection method and the potential for bias when using this method before using the data to extrapolate new information or drawing conclusions.

Incomplete or inaccurate data sets ● (core concept) — Some data sets are incomplete or contain inaccurate data. Using such data in the development or use of a program can cause the program to work incorrectly or inefficiently.

Choosing an appropriate data set ● (core concept) — Contents of a data set might be related to a specific question or topic and might not be appropriate to give correct answers or extrapolate information for a different question or topic.

4.2 Introduction to Using Data Sets

Data set ● (core concept) — A data set is a collection of specific pieces of information or data.

Analyzing data sets ● (core concept) — Data sets can be manipulated and analyzed to solve a problem or answer a question. When analyzing data sets, values within the set are accessed and utilized one at a time and then processed according to the desired outcome.

Data set diagram (chart or table) ● (core concept) — Data can be represented in a diagram by using a chart or table. This visual can be used to plan the algorithm that will be used to manipulate the data.

4.3 Array Creation and Access

Array ● (core concept) — An array stores multiple values of the same type. The values can be either primitive values or object references.

Array length (fixed) ● (core concept) — The length of an array is established at the time of creation and cannot be changed. The length of an array can be accessed through the length attribute.

Array default initialization ● (core concept) — When an array is created using the keyword new, all of its elements are initialized to the default values for the element data type. The default value for int is 0, for double is 0.0, for boolean is false, and for a reference type is null.

Initializer list ● (core concept) — Initializer lists can be used to create and initialize arrays.

Array element access ● (core concept) — Square brackets [] are used to access and modify an element in a 1D array using an index.

Array index range ● (core concept) — The valid index values for an array are 0 through one less than the length of the array, inclusive. Using an index value outside of this range will result in an ArrayIndexOutOfBoundsException.

4.4 Array Traversals

Traversing an array ● (core concept) — Traversing an array is when repetition statements are used to access all or an ordered sequence of elements in an array.

Indexed traversal ● (core concept) — Traversing an array with an indexed for loop or while loop requires elements to be accessed using their indices.

Enhanced for loop ● (core concept) — An enhanced for loop header includes a variable, referred to as the enhanced for loop variable. For each iteration of the enhanced for loop, the enhanced for loop variable is assigned a copy of an element without using its index.

Enhanced for loop variable assignment ● (core concept) — Assigning a new value to the enhanced for loop variable does not change the value stored in the array.

Enhanced for loop with object references ● (core concept) — When an array stores object references, the attributes can be modified by calling methods on the enhanced for loop variable. This does not change the object references stored in the array.

Enhanced/indexed/while traversal equivalence ● (core concept) — Code written using an enhanced for loop to traverse elements in an array can be rewritten using an indexed for loop or a while loop.

4.5 Implementing Array Algorithms

Standard array algorithms ● (core concept) — Standard algorithms that utilize array traversals include: determining a minimum or maximum value; computing a sum or average; determining if at least one element has a particular property; determining if all elements have a particular property; determining the number of elements having a particular property; accessing all consecutive pairs of elements; determining the presence or absence of duplicate elements; shifting or rotating elements left or right; and reversing the order of the elements.

4.6 Using Text Files

File ● (core concept) — A file is storage for data that persists when the program is not running. The data in a file can be retrieved during program execution.

File and Scanner classes ● (core concept) — A file can be connected to the program using the File and Scanner classes.

File(String) constructor ● (core concept) — A file can be opened by creating a File object, using the name of the file as the argument of the constructor. File(String str) is the File constructor that accepts a String file name to open for reading, where str is the pathname for the file.

throws IOException ● (core concept) — When using the File class, it is required to indicate what to do if the file with the provided name cannot be opened. One way to accomplish this is to add throws IOException to the header of the method that uses the file. If the file name is invalid, the program will terminate.

java.io package ● (core concept) — The File and IOException classes are part of the java.io package. An import statement must be used to make these classes available for use in the program.

Scanner methods (file reading) ● (core concept) — The Scanner methods on the Java Quick Reference are: Scanner(File f), the Scanner constructor that accepts a File for reading; int nextInt(), which returns the next int read from the file or input source (InputMismatchException if the next int does not exist or is out of range); double nextDouble(), which returns the next double read (InputMismatchException if the next double does not exist); boolean nextBoolean(), which returns the next boolean read (InputMismatchException if the next boolean does not exist); String nextLine(), which returns the next line of text as a String (it can return the empty string if called immediately after another Scanner method); String next(), which returns the next String read; boolean hasNext(), which returns true if there is a next item to read; and void close(), which closes this scanner.

Mixing nextLine with other Scanner methods ● (core concept) — Using nextLine and the other Scanner methods together on the same input source sometimes requires code to adjust for the methods' different ways of handling whitespace. (Writing or analyzing code that mixes nextLine with other Scanner methods on the same input source is outside the scope of the course and exam.)

String.split ● (core concept) — String[] split(String del) returns a String array where each element is a substring of this String, which has been split around matches of the given expression del.

Reading a file with a while loop ● (core concept) — A while loop can be used to detect if the file still contains elements to read by using the hasNext method as the condition of the loop.

Closing a file ● (core concept) — A file should be closed when the program is finished using it. The close method from Scanner is called to close the file.

4.7 Wrapper Classes

Integer and Double wrapper classes ● (core concept) — The Integer class and Double class are part of the java.lang package. An Integer object is immutable, meaning once an Integer object is created, its attributes cannot be changed. A Double object is immutable, meaning once a Double object is created, its attributes cannot be changed.

Autoboxing ● (core concept) — Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class, or assigned to a variable of the corresponding wrapper class.

Unboxing ● (core concept) — Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is passed as a parameter to a method that expects a value of the corresponding primitive type, or assigned to a variable of the corresponding primitive type.

Integer.parseInt ● (core concept) — static int parseInt(String s) returns the String argument as an int.

Double.parseDouble ● (core concept) — static double parseDouble(String s) returns the String argument as a double.

4.8 ArrayList Methods

ArrayList ● (core concept) — An ArrayList object is mutable in size and contains object references.

ArrayList constructor ● (core concept) — The ArrayList constructor ArrayList() constructs an empty list.

Generic ArrayList<E> ● (core concept) — Java allows the generic type ArrayList<E>, where the type parameter E specifies the type of the elements. When ArrayList<E> is specified, the types of the reference parameters and return type when using the ArrayList methods are type E. ArrayList<E> is preferred over ArrayList. For example, ArrayList<String> names = new ArrayList<String>(); allows the compiler to find errors that would otherwise be found at run time.

java.util package (ArrayList import) ● (core concept) — The ArrayList class is part of the java.util package. An import statement must be used to make this class available for use in the program.

ArrayList.size() ● (core concept) — int size() returns the number of elements in the list.

ArrayList.add ● (core concept) — boolean add(E obj) appends obj to end of list and returns true. void add(int index, E obj) inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size.

ArrayList.get ● (core concept) — E get(int index) returns the element at position index in the list.

ArrayList.set ● (core concept) — E set(int index, E obj) replaces the element at position index with obj; returns the element formerly at position index.

ArrayList.remove ● (core concept) — E remove(int index) removes the element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index.

ArrayList index range ● (core concept) — The indices for an ArrayList start at 0 and end at the number of elements - 1.

4.9 ArrayList Traversals

Traversing an ArrayList ● (core concept) — Traversing an ArrayList is when iteration or recursive statements are used to access all or an ordered sequence of the elements in an ArrayList.

Deleting during ArrayList traversal ● (core concept) — Deleting elements during a traversal of an ArrayList requires the use of special techniques to avoid skipping elements.

IndexOutOfBoundsException ● (core concept) — Attempting to access an index value outside of its range will result in an IndexOutOfBoundsException.

ConcurrentModificationException ● (core concept) — Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException. Therefore, when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements.

4.10 Implementing ArrayList Algorithms

Standard ArrayList algorithms ● (core concept) — Standard ArrayList algorithms that utilize traversals include: determining a minimum or maximum value; computing a sum or average; determining if at least one element has a particular property; determining if all elements have a particular property; determining the number of elements having a particular property; accessing all consecutive pairs of elements; determining the presence or absence of duplicate elements; shifting or rotating elements left or right; reversing the order of the elements; inserting elements; and deleting elements.

Simultaneous traversals ● (core concept) — Some algorithms require multiple String, array, or ArrayList objects to be traversed simultaneously.

4.11 2D Array Creation and Access

2D array (array of arrays) ● (core concept) — A 2D array is stored as an array of arrays. Therefore, the way 2D arrays are created and indexed is similar to 1D array objects. The size of a 2D array is established at the time of creation and cannot be changed. 2D arrays can store either primitive data or object reference data.

2D array default initialization ● (core concept) — When a 2D array is created using the keyword new, all of its elements are initialized to the default values for the element data type. The default value for int is 0, for double is 0.0, for boolean is false, and for a reference type is null.

2D initializer list ● (core concept) — The initializer list used to create and initialize a 2D array consists of initializer lists that represent 1D arrays; for example, int[][] arr2D = { {1, 2, 3}, {4, 5, 6} };.

2D array element access [row][col] ● (core concept) — The square brackets [row][col] are used to access and modify an element in a 2D array. For the purposes of the exam, when accessing the element at arr[first][second], the first index is used for rows and the second index is used for columns.

Accessing a row of a 2D array ● (core concept) — A single array that is a row of a 2D array can be accessed using the 2D array name and a single set of square brackets containing the row index.

2D array dimensions via length ● (core concept) — The number of rows contained in a 2D array can be accessed through the length attribute (values.length), and the number of columns through the length attribute of one of the rows (values[0].length). The valid row index values are 0 through one less than the number of rows, inclusive, and the valid column index values are 0 through one less than the number of columns, inclusive. Using an index value outside of these ranges will result in an ArrayIndexOutOfBoundsException.

4.12 2D Array Traversals

Row-major vs. column-major order ● (core concept) — Nested iteration statements can be written to traverse the 2D array in row-major order, column-major order, or a uniquely defined order. Row-major order refers to an ordering of 2D array elements where traversal occurs across each row, whereas column-major order traversal occurs down each column.

Nested enhanced for loop over a 2D array ● (core concept) — The outer loop of a nested enhanced for loop used to traverse a 2D array traverses the rows. Therefore, the enhanced for loop variable must be the type of each row, which is a 1D array. The inner loop traverses a single row. Therefore, the inner enhanced for loop variable must be the same type as the elements stored in the 1D array.

4.13 Implementing 2D Array Algorithms

Standard 2D array algorithms ● (core concept) — Standard algorithms that utilize 2D array traversals include: determining a minimum or maximum value of all the elements or for a designated row, column, or other subsection; computing a sum or average of all the elements or for a designated row, column, or other subsection; determining if at least one element has a particular property in the entire 2D array or for a designated row, column, or other subsection; determining if all elements of the 2D array or a designated row, column, or other subsection have a particular property; determining the number of elements in the 2D array or in a designated row, column, or other subsection having a particular property; accessing all consecutive pairs of elements; determining the presence or absence of duplicate elements in the 2D array or in a designated row, column, or other subsection; shifting or rotating elements in a row left or right or in a column up or down; and reversing the order of the elements in a row or column.

4.14 Searching Algorithms

Linear search ● (core concept) — Linear search algorithms are standard algorithms that check each element in order until the desired value is found or all elements in the array or ArrayList have been checked. Linear search algorithms can begin the search process from either end of the array or ArrayList.

Linear search on a 2D array ● (core concept) — When applying linear search algorithms to 2D arrays, each row must be accessed then linear search applied to each row of the 2D array.

4.15 Sorting Algorithms

Selection sort and insertion sort ● (core concept) — Selection sort and insertion sort are iterative sorting algorithms that can be used to sort elements in an array or ArrayList.

Selection sort ● (core concept) — Selection sort repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it into its correct (and final) position in the sorted portion of the list.

Insertion sort ● (core concept) — Insertion sort inserts an element from the unsorted portion of a list into its correct (but not necessarily final) position in the sorted portion of the list by shifting elements of the sorted portion to make room for the new element.

4.16 Recursion

Recursive method ● (core concept) — A recursive method is a method that calls itself. Recursive methods contain at least one base case, which halts the recursion, and at least one recursive call. Recursion is another form of repetition.

Base case ● (core concept) — The base case of a recursive method is the case that halts the recursion. Every recursive method contains at least one base case.

Recursive call state ● (core concept) — Each recursive call has its own set of local variables, including the parameters. Parameter values capture the progress of a recursive process, much like loop control variable values capture the progress of a loop.

Recursion/iteration equivalence ● (core concept) — Any recursive solution can be replicated through the use of an iterative approach and vice versa.

4.17 Recursive Searching and Sorting

Recursion over strings and collections ● (core concept) — Recursion can be used to traverse String objects, arrays, and ArrayList objects.

Binary search ● (core concept) — Data must be in sorted order to use the binary search algorithm. Binary search starts at the middle of a sorted array or ArrayList and eliminates half of the array or ArrayList in each recursive call until the desired value is found or all elements have been eliminated.

Binary search efficiency ● (core concept) — Binary search is typically more efficient than linear search.

Binary search iterative or recursive ● (core concept) — The binary search algorithm can be written either iteratively or recursively.

Merge sort ● (core concept) — Merge sort is a recursive sorting algorithm that can be used to sort elements in an array or ArrayList.

Merge sort process ● (core concept) — Merge sort repeatedly divides an array into smaller subarrays until each subarray is one element and then recursively merges the sorted subarrays back together in sorted order to form the final sorted array.