|  | Home Page | Array Lists Intro/Methods | Traversing | Searching & Algorithms | Sorting | Ethics | Hacks | 
Unit 7 ArrayList P3
A Unit of documents that overview Array Lists in Java
ArrayList Coding Activity
Objective:
Students will create, manipulate, and sort an ArrayList of integers.
Activity Outline:
- Create an ArrayList:
    - Task: Create an ArrayListof integers and add 5 elements of your choice.
- Hint: Use the ArrayListclass and the.add()method to add elements.
 
- Task: Create an 
- Modify an Element:
    - Task: Change the second element (index 1) of the ArrayListto a new value (e.g., 100).
- Hint: The .set()method allows you to update an element at a specific index.
 
- Task: Change the second element (index 1) of the 
- Remove an Element:
    - Task: Remove the third element (index 2) from the ArrayList.
- Hint: Use the .remove()method to delete an element by its index.
 
- Task: Remove the third element (index 2) from the 
- Search for an Element:
    - Task: Check if a specific number (e.g., 30) is in the ArrayListand print a message based on whether it is found or not.
- Hint: The .contains()method can be used to check for the presence of an element.
 
- Task: Check if a specific number (e.g., 30) is in the 
- Loop Through the ArrayList:
    - Task: Use a forloop to print each element of theArrayList.
- Hint: You can use a traditional forloop or an enhancedforloop (for-each) to iterate through the elements.
 
- Task: Use a 
- Sort the ArrayList:
    - Task: Sort the ArrayListin ascending order.
- Hint: Use Collections.sort()to sort theArrayList.
 
- Task: Sort the 
- Print the Sorted ArrayList:
    - Task: Print the sorted ArrayListto see the updated order of the elements.
- Hint: Use System.out.println()to print the sorted list.
 
- Task: Print the sorted 
import java.util.ArrayList;
import java.util.Collections;
public class HackYum {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(20);
        numbers.add(10);
        numbers.add(40);
        numbers.add(30);
        numbers.add(50);
        
        System.out.println("Original ArrayList: " + numbers);
        numbers.set(1, 45678);
        System.out.println("After modifying second element: " + numbers);
        numbers.remove(2);
        System.out.println("After removing the third element: " + numbers);
        int searchValue = 30;
        if (numbers.contains(searchValue)) {
            System.out.println(searchValue + " is in the ArrayList.");
        } else {
            System.out.println(searchValue + " is not in the ArrayList.");
        }
        System.out.println("Looping through the ArrayList:");
        for (int number : numbers) {
            System.out.println(number);
        }
        Collections.sort(numbers);
        System.out.println("Sorted ArrayList: " + numbers);
    }
}
HackYum.main(null);
Original ArrayList: [20, 10, 40, 30, 50]
After modifying second element: [20, 45678, 40, 30, 50]
After removing the third element: [20, 45678, 30, 50]
30 is in the ArrayList.
Looping through the ArrayList:
20
45678
30
50
Sorted ArrayList: [20, 30, 50, 45678]