1 Answers
π Quick Study Guide: AP CSA Data Structures (Java)
- π Arrays:
- β Fixed-size, contiguous collection of elements of the same data type (primitives or objects).
- βοΈ Declaration: `dataType[] arrayName = new dataType[size];` Example: `int[] numbers = new int[5];`
- π Access elements using an index: `arrayName[index]`. Indices range from 0 to `length - 1`.
- π’ Length is accessed via `.length` (e.g., `numbers.length`).
- π ArrayLists:
- β¨ Dynamic-size, resizable array-like structure that stores objects (cannot store primitives directly).
- π Declaration: `ArrayList
listName = new ArrayList ();` Example: `ArrayList names = new ArrayList ();` - βοΈ Key Methods:
- β `add(element)`: Appends an element to the end.
- π `add(index, element)`: Inserts an element at a specific index.
- β‘οΈ `get(index)`: Returns the element at the specified index.
- π `set(index, element)`: Replaces the element at the specified index.
- βοΈ `remove(index)`: Removes the element at the specified index and shifts subsequent elements.
- π `size()`: Returns the number of elements in the list.
- πΌοΈ 2D Arrays (Matrices):
- π An array of arrays, representing rows and columns.
- π Declaration: `dataType[][] matrixName = new dataType[rows][cols];` Example: `int[][] grid = new int[3][4];`
- π― Access elements using two indices: `matrixName[rowIndex][colIndex]`.
- π Row length: `matrixName.length`. Column length of a specific row: `matrixName[rowIndex].length`.
- βοΈ Key Differences:
- π Arrays have a fixed size; π ArrayLists are dynamic.
- π¦ Arrays can store primitives and objects; β»οΈ ArrayLists only store objects (use wrapper classes for primitives).
- π Arrays use `.length` for size; π ArrayLists use `.size()` method.
π Practice Quiz
Choose the best answer for each question.
1. Which of the following is the correct way to declare and initialize an array of 10 integers in Java?
- `int[] numbers = {10};`
- `int numbers[] = new int[10];`
- `int numbers = new int[10];`
- `Array
numbers = new Array (10);`
2. What will be the output after executing the following Java code snippet?
ArrayList list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add(0, "Cherry");
System.out.println(list.get(1));
- `Apple`
- `Banana`
- `Cherry`
- `IndexOutOfBoundsException`
3. Given a 2D array `int[][] matrix = {{1, 2, 3}, {4, 5, 6}};`, what is the value of `matrix[1][0]`?
- `1`
- `2`
- `4`
- `5`
4. What is a primary advantage of using an `ArrayList` over a traditional Java array?
- `ArrayLists` can store primitive data types directly.
- `ArrayLists` have a fixed size, which improves performance.
- `ArrayLists` can dynamically resize themselves.
- `ArrayLists` allow direct access to elements without using an index.
5. Consider the following `ArrayList`:
`ArrayList
`nums.add(10); nums.add(20); nums.add(30);`
What is the content of `nums` after `nums.remove(1);` is executed?
- `[10, 30]`
- `[20, 30]`
- `[10, 20]`
- `[10, 20, 30]` (no change)
6. Which of the following loops correctly iterates through all elements of an `int[] arr = {1, 2, 3, 4, 5};`?
-
for (int i = 1; i <= arr.length; i++) { System.out.println(arr[i]); } -
for (int num : arr) { System.out.println(num); } -
for (int i = 0; i < arr.length - 1; i++) { System.out.println(arr[i]); } -
for (int i = 0; i <= arr.length; i++) { System.out.println(arr[i]); }
7. Given a 2D array `String[][] names = new String[5][3];`, what does `names.length` represent?
- The total number of elements in the array.
- The number of columns (3).
- The number of rows (5).
- The length of the first row (which is 3).
Click to see Answers
1. B
2. A (List becomes ["Cherry", "Apple", "Banana"], `list.get(1)` is "Apple")
3. C
4. C
5. A (Original: [10, 20, 30]. Remove index 1 (20) -> [10, 30])
6. B
7. C
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! π