ArrayList Coding Activity

Objective:

Students will create, manipulate, and sort an ArrayList of integers.

Activity Outline:

  1. Create an ArrayList:
    • Task: Create an ArrayList of integers and add 5 elements of your choice.
    • Hint: Use the ArrayList class and the .add() method to add elements.
  2. Modify an Element:
    • Task: Change the second element (index 1) of the ArrayList to a new value (e.g., 100).
    • Hint: The .set() method allows you to update an element at a specific index.
  3. 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.
  4. Search for an Element:
    • Task: Check if a specific number (e.g., 30) is in the ArrayList and 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.
  5. Loop Through the ArrayList:
    • Task: Use a for loop to print each element of the ArrayList.
    • Hint: You can use a traditional for loop or an enhanced for loop (for-each) to iterate through the elements.
  6. Sort the ArrayList:
    • Task: Sort the ArrayList in ascending order.
    • Hint: Use Collections.sort() to sort the ArrayList.
  7. Print the Sorted ArrayList:
    • Task: Print the sorted ArrayList to see the updated order of the elements.
    • Hint: Use System.out.println() to print the sorted list.