![]() |
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
