AP Computer Science A Unit 7 Review with example codes….
1. Introduction to ArrayList
import java.util.ArrayList;
public class ArrayListIntroductionExample {
public static void main(String[] args) {
// Creating an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<>();
// Adding elements to the ArrayList
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
// Displaying the ArrayList
System.out.println("ArrayList: " + numbers);
}
}
2. ArrayList Methods
import java.util.ArrayList;
public class ArrayListMethodsExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
// Adding elements to the ArrayList
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Getting the size of the ArrayList
System.out.println("Size: " + fruits.size()); // Output: 3
// Accessing elements using indices
System.out.println("First element: " + fruits.get(0)); // Output: Apple
// Removing an element by index
fruits.remove(1); // Remove Banana
// Checking if an element is in the ArrayList
System.out.println("Contains Orange: " + fruits.contains("Orange")); // Output: true
// Displaying the updated ArrayList
System.out.println("Updated ArrayList: " + fruits);
}
}
3. Traversing ArrayLists
import java.util.ArrayList;
public class ArrayListTraversingExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Traditional for loop to traverse the ArrayList
System.out.println("Using traditional for loop:");
for (int i = 0; i < fruits.size(); i++) {
System.out.println("Element at index " + i + ": " + fruits.get(i));
}
// Enhanced for loop (for-each) to traverse the ArrayList
System.out.println("\nUsing enhanced for loop:");
for (String fruit : fruits) {
System.out.println("Element: " + fruit);
}
}
}
4. Developing Algorithms Using ArrayLists
import java.util.ArrayList;
public class ArrayListAlgorithmsExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(15);
numbers.add(3);
numbers.add(8);
numbers.add(23);
numbers.add(7);
numbers.add(12);
// Finding the maximum value in the ArrayList
int max = numbers.get(0);
for (int i = 1; i < numbers.size(); i++) {
if (numbers.get(i) > max) {
max = numbers.get(i);
}
}
System.out.println("Maximum value in the ArrayList: " + max);
// Finding the minimum value in the ArrayList
int min = numbers.get(0);
for (int i = 1; i < numbers.size(); i++) {
if (numbers.get(i) < min) {
min = numbers.get(i);
}
}
System.out.println("Minimum value in the ArrayList: " + min);
// Calculating the sum of the ArrayList elements
int sum = 0;
for (int number : numbers) {
sum += number;
}
System.out.println("Sum of ArrayList elements: " + sum);
}
}
5. Searching
import java.util.ArrayList;
public class ArrayListSearchingExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Checking if an element is in the ArrayList
boolean containsAlice = names.contains("Alice");
System.out.println("Contains Alice: " + containsAlice); // Output: true
boolean containsEve = names.contains("Eve");
System.out.println("Contains Eve: " + containsEve); // Output: false
}
}
6. Sorting
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListSortingExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Charlie");
names.add("Alice");
names.add("Bob");
// Sorting the ArrayList
Collections.sort(names);
// Displaying the sorted ArrayList
System.out.println("Sorted ArrayList: " + names);
}
}