Search
• Master Hun

# Master Hun's [AP Computer Science A] Array & ArrayList Notes

#APComputerScienceA #APComputerScience #AdvancedPlacement Topic: Array, ArrayList An array is a collection of variables of the same data type. An array is zero-based. The first item or element in the array has the index 0. Not 1 as non-coders would assume. All the elements of an array must be of the same data type. Follow this formula to create an array in Java: dataType[] arrayName = dataType[numberOfElements]; For example: int[] arrExample = int; If you already know the values you want the array to hold, you can code the array as follows: int[] arrExample = {4, 5, 6, 8, 9}; To access/change an element of an array, use the formula arrayName[index] For example: System.out.println(arrExample); // Output: 4 Or: arrExample = 0; // arrExample is now {4, 5, 0, 8, 9} Note: The length of an array is fixed. Once the array is created, its length can not be increased or decreased. To find the length of an array use the formula arrayName.length For example: System.out.println(arrExample.length); // Output: 5 What two goes together perfectly? The for loop & the array For example: int[] numberArray = {10, 3, 6, 4, 5, 1}; for (int i = 0; i < numberArray.length; i++) { System.out.println(numberArray[i]); } // To print out each element in this array // To total all the elements, to find the sum int[] numberArray = {10, 3, 6, 4, 5, 1}; int sumArray = 0; for (var i = 0; i < numberArray.length; i++) { sumArray += numberArray[i]; } System.out.println("Total: " + sumArray); Array Exercises A. How do we code a 2D array of integers? Code a 3 by 3 2D array of integers. B. How to we "iterate" (or traverse) a 2D array? Using the above 2D array, traverse the 2D array, and display each row on its own line. ArrayList: Array "NEW & IMPROVED!" Arrays are useful. But arrays can be "NEW & IMPROVED!" The "NEW & IMPROVED!" ArrayList is more flexible (i.e., dynamic length) and has more functionality (i.e, methods) Unlike an array, an ArrayList can add() and remove() elements dynamically. Unlike an array, an ArrayList must be objects. And all the objects in the same ArrayList must be of the same type. Like Integer, Double, String Not primitive data types: int or double To code an ArrayList follow this formula: ArrayList<type> variableName = new ArrayList<type>(); For example: ArrayList<Integer> numArr = new ArrayList<Integer>(); ArrayList Methods To add an element to an array use the formula: listName.add(value); For example: numArr.add(5); // In this case, add() appends 5 to the ArrayList numArr. // Appends means to add at the end. To add an element to an array at a specific index, follow this formula: numArr.add(2,5); // Adds 5 at index 2 (the 3rd element) of the ArrayList. // And shifts all the following elements to the right. To access an element, use the get() method (i.e., a getter method). For example: System.out.println(numArr.get(2)); // Output: 5 To set or change an elment, use the set() method (i.e., a setter method). For example: numArr.set(2,0); // Changes element at index 2 to the value (integer) 0. Unlike length with arrays, use the size() method with ArrayList objects. For example: System.out.println(numArr.size()); // Output: 5 (int date type) To remove an element from an ArrayList, use the remove() method. For example: numArr.remove(2); // Removes the element at index 2. // As a result, the size of the ArrayList numArr decreases by 1 element. Warning! DO NOT use the for each loop when removing an item (or element) from an ArrayList. The for each loop does not use an index. Instead use a while loop, for example: int index = 0; while (index < numArr.size()) { if (numArr[index] <= 0) numArr.remove(index); // Remove Integer objects with negative and zero values else index++; // Only increment when not removing an element } To "iterate" over an ArrayList, you can use a regular "for loop". Or you can use the "NEW & IMPROVED!" for each loop. For example: for (int x: numArr) { System.out.println(x); } // Each element of numArr is displayed on its own line. To "iterate" over (or traverse) an ArrayList with a for loop. For example: // Compiled and tested with SoloLearn's Code Playground import java.util.ArrayList; public class TestClass { public static void main(String[] args) { ArrayList<Integer> numArr = new ArrayList<Integer>(); numArr.add(1); numArr.add(2); numArr.add(3); for (int i = 0; i < numArr.size(); i++ ) { System.out.println(numArr.get(i)); // Displays each Integer value on its own line. } } } Warning! DO NOT confuse IndexOutOfBoundsException vs. ArrayIndexOutOfBoundsException errors. With an array, if you use an index that is out of range (i.e., does not exist), then you will generate an ArrayIndexOutOfBoundsException. With an ArrayList, you will generate an IndexOutOfBoundsException. ArrayList Exercise Code a 2D array of integers, with at least 3 rows and 3 columns. Then code a nested for loop to sum up all the integers. The sum of the 2D array should be displayed.