Question:
Suppose you are working on a project that requires storing and manipulating a collection of elements. You decide to use an ArrayList
to dynamically manage this collection. Demonstrate your understanding of the basic functionalities of ArrayList
by completing the following tasks:
ArrayList
called numbers
that can store integers.numbers
list: 10, 20, 30, 40, 50.numbers
list.numbers
list.numbers
list contains the number 30.ArrayList
called numbers2
and add all the elements from the numbers
list to the numbers2
list.numbers
list.numbers
list and the numbers2
list to verify the changes.Write a Java program to perform the above tasks. Make sure to include appropriate variable declarations and output statements.
Answer:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// 1. Create an ArrayList called numbers that can store integers.
ArrayList<Integer> numbers = new ArrayList<>();
// 2. Add the following numbers to the numbers list: 10, 20, 30, 40, 50.
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
// 3. Print the size of the numbers list.
System.out.println("Size of numbers list: " + numbers.size());
// 4. Change the value at index 2 to 35.
numbers.set(2, 35);
// 5. Remove the number 40 from the numbers list.
numbers.remove(Integer.valueOf(40));
// 6. Check if the numbers list contains the number 30.
boolean contains30 = numbers.contains(30);
System.out.println("Numbers list contains 30: " + contains30);
// 7. Create a new ArrayList called numbers2 and add all the elements from the numbers list to the numbers2 list.
ArrayList<Integer> numbers2 = new ArrayList<>(numbers);
// 8. Clear all the elements from the numbers list.
numbers.clear();
// 9. Print the size of the numbers list and the numbers2 list to verify the changes.
System.out.println("Size of numbers list after clearing: " + numbers.size());
System.out.println("Size of numbers2 list: " + numbers2.size());
}
}
Explanation:
ArrayList
called numbers
that stores integers using the ArrayList<Integer>
syntax.numbers
list using the add()
method.size()
method to print the size of the numbers
list.set()
method to change the value at index 2 (which is 30) to 35.numbers
list using the remove()
method. Since remove()
takes an object as input, we use Integer.valueOf(40)
to convert the int
40 to an Integer
object.contains()
method to check if the numbers
list contains the number 30 and store the result in the contains30
variable.ArrayList
called numbers2
and pass the numbers
list as a parameter to the constructor of ArrayList
to copy all the elements from numbers
to numbers2
.clear()
method to remove all the elements from the numbers
list.size()
method again to print the size of the numbers
list after clearing it and the size of the numbers2
list to verify the changes made.The output of the program would be:
Size of numbers list: 5
Numbers list contains 30: false
Size of numbers list after clearing: 0
Size of numbers2 list: 4