ArrayList Operations Lab
ArrayList Operations Lab
In this lab, you’ll explore the fundamental operations of ArrayLists through a series of coding exercises. Complete each task and observe how ArrayLists behave differently from regular arrays.
Setup
Create a new class called ArrayListLab
with a main method. Use this structure for your code:
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListLab {
public static void main(String[] args) {
// Your code will go here
// Print results after each task
System.out.println("Task 1 results:");
// etc...
}
}
Tasks
Task 1: Creating and Adding Elements
Create an ArrayList of Strings called fruits
. Add these fruits in order: “apple”, “banana”, “orange”.
Then insert “mango” at index 1.
Expected output:
[apple, mango, banana, orange]
Task 2: Exploring ArrayList Size
- Create an ArrayList of Integers
- Add the numbers 1 through 5
- Print the size
- Remove two elements
- Print the size again
- Add three more elements
- Print final size
This task demonstrates how ArrayLists automatically resize.
Task 3: Finding and Replacing
Create an ArrayList with the following names: [“Alice”, “Bob”, “Charlie”, “Bob”, “David”]
- Find the first index of “Bob”
- Find the last index of “Bob”
- Replace the first “Bob” with “Robert”
- Print the modified list
Task 4: Removing Elements
Create an ArrayList with numbers 1-5. Write code to:
- Remove the element at index 2
- Remove the first occurrence of the number 4
- Print the list after each removal
Task 5: Working with Subsets
Create an ArrayList with the numbers 1 through 10. Then:
- Get a subList from index 3 to 7
- Print both the original list and the subList
- Modify an element in the subList
- Print both lists again to see how they’re connected
Challenge Task: Filtering
Create an ArrayList of integers with values [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Write code to:
- Remove all odd numbers
- Print the resulting list
- Try to do this using a loop (Hint: be careful with indexes when removing elements!)
Example Solution for Task 1
ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("orange");
fruits.add(1, "mango");
System.out.println(fruits);