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;
ArrayList<Integer> myIntegerList = new ArrayList<Integer>();
System.out.println("Original ArrayList: " + myIntegerList);
myIntegerList.add(35);
myIntegerList.add(53);
myIntegerList.add(78);
myIntegerList.add(24);
myIntegerList.add(18);
System.out.println("ArrayList with new elements freshly added: " + myIntegerList);
Original ArrayList: []
ArrayList with new elements freshly added: [35, 53, 78, 24, 18]
System.out.println("Third element before change: " + myIntegerList.get(2));
myIntegerList.set(2, 59);
System.out.println("Third element after change: " + myIntegerList.get(2));
Third element before change: 78
Third element after change: 59
System.out.println("ArrayList before removing second element: " + myIntegerList);
myIntegerList.remove(1); //removes second element which is 53, from the ArrayList
System.out.println("ArrayList after removing second element: " + myIntegerList);
ArrayList before removing second element: [35, 53, 59, 24, 18]
ArrayList after removing second element: [35, 59, 24, 18]
int searchNumber = 30;
if (myIntegerList.contains(searchNumber)) {
System.out.println(searchNumber + " is found in the ArrayList.");
} else {
System.out.println(searchNumber + " is not found in the ArrayList.");
}
int searchNumber = 59;
if (myIntegerList.contains(searchNumber)) {
System.out.println(searchNumber + " is found in the ArrayList.");
} else {
System.out.println(searchNumber + " is not found in the ArrayList.");
}
30 is not found in the ArrayList.
59 is found in the ArrayList.
for(int number : myIntegerList) {
System.out.println(number);
}
35
59
24
18
import java.util.Collections;
Collections.sort(myIntegerList);
System.out.println(myIntegerList);
[18, 24, 35, 59]