1/25
Flashcards for reviewing Java Arrays and ArrayLists, including 2D arrays, ArrayList algorithms, comparisons between Arrays and ArrayLists, and array algorithms.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Java Array
A data structure that stores a fixed-size sequential collection of elements of the same type.
Java ArrayList
A dynamic array implementation in Java that can grow or shrink in size. Part of Java's Collection framework.
2D Arrays
Declaration: int[][] things = new int[3][6]; (3 rows, 6 columns)
Rows
The number of rows in a 2D array.
Columns
The number of columns in a specific row (e.g., row 0) of a 2D array.
Accessing elements (valid)
Examples: things[0][0], things[0][3], things[1][0] to things[1][5], things[2][1] to things[2][4]
Accessing elements (invalid)
Examples: things[8][1], things[2][9], things[@][2], things[8][53]
Traversing 2D Arrays
Nested for-loop: for (int i = 0; i < things.length; i++) { for (int j = 0; j < things[0].length; j++) { System.out.println(things[i][j]); } }
Rule for Traversing 2D Arrays
Rows = outer loop, Columns = inner loop
ArrayList Algorithms - Remove Items
Backward loop: for (int i = cars.size() - 1; i >= 0; i--) { if (cars.get(i).getColor().equals("red")) { cars.remove(i); } }
ArrayList Algorithms - Find Index of Item
For loop: for (int i = 0; i < cars.size(); i++) { if (cars.get(i).getColor().equals("red")) return i; } return -1;
ArrayList Algorithms - Find Sum
For loop: int sum = 0; for (int i = 0; i < cars.size(); i++) { sum += cars.get(i).getWeight(); } System.out.println(sum);
Traversing ArrayLists
Enhanced for-loop: for (Car c : cars) { System.out.println(c.getName()); }
Traversing ArrayLists
Regular for-loop: for (int i = 0; i < cars.size(); i++) { System.out.println(cars.get(i).getName()); }
Access (Arrays vs ArrayLists)
arr[0] vs arrayList.get(0)
Set item (Arrays vs ArrayLists)
arr[0] = "elephant" vs arrayList.set(0, "elephant")
Size (Arrays vs ArrayLists)
arr.length vs arrayList.size()
Initialize (Arrays vs ArrayLists)
String[] arr = {...} vs new ArrayList<String>()
Add item (Arrays vs ArrayLists)
Not allowed vs arrayList.add("giraffe")
Remove item (Arrays vs ArrayLists)
Not allowed vs arrayList.remove(0)
Array Algorithms - Find Average
For loop: double total = 0; for (int i = 0; i < items.length; i++) { total += items[i]; } System.out.println(total / items.length);
Array Algorithms - Find Item
For loop: boolean found = false; for (int i = 0; i < items.length; i++) { if (items[i] == 42) found = true; } System.out.println(found);
Array Algorithms - Find Max
For loop: int max = Integer.MIN_VALUE; for (int i = 0; i < items.length; i++) { if (items[i] > max) max = items[i]; } System.out.println(max);
Looping in Reverse (Array)
for (int i = animals.length - 1; i >= 0; i--) { System.out.println(animals[i]); }
For-each Loop (Enhanced For)
for (String animal : animals) { System.out.println(animal); }
For Loop with Index (Arrays)
for (int i = 0; i < animals.length; i++) { System.out.println(animals[i]); }