Post

Created by @nathanedwards
 at November 1st 2023, 2:22:16 am.

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:

  1. Create an ArrayList called numbers that can store integers.
  2. Add the following numbers to the numbers list: 10, 20, 30, 40, 50.
  3. Print the size of the numbers list.
  4. Change the value at index 2 to 35.
  5. Remove the number 40 from the numbers list.
  6. Check if the numbers list contains the number 30.
  7. Create a new ArrayList called numbers2 and add all the elements from the numbers list to the numbers2 list.
  8. Clear all the elements from the numbers list.
  9. Print the size of the 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:

  1. We declare an ArrayList called numbers that stores integers using the ArrayList<Integer> syntax.
  2. We add the numbers 10, 20, 30, 40, and 50 to the numbers list using the add() method.
  3. We use the size() method to print the size of the numbers list.
  4. We use the set() method to change the value at index 2 (which is 30) to 35.
  5. We remove the number 40 from the 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.
  6. We use the contains() method to check if the numbers list contains the number 30 and store the result in the contains30 variable.
  7. We create a new 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.
  8. We use the clear() method to remove all the elements from the numbers list.
  9. We use the 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