Post

Created by @nathanedwards
 at November 1st 2023, 1:56:06 am.

Question:

Consider the following code snippet:

import java.util.ArrayList;

public class ArrayListOperations {
    
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(5);
        numbers.add(10);
        numbers.add(7);
        numbers.add(3);
        numbers.add(9);
        
        // Perform operations on the 'numbers' ArrayList
        
    }
}

Implement the following ArrayList operations based on the given code snippet:

  1. Print the number of elements in the ArrayList.
  2. Check if the ArrayList is empty.
  3. Retrieve and print the element at index position 3.
  4. Replace the element at index position 1 with a new value of 15.
  5. Remove the element at index position 2.
  6. Add a new element with the value 12 at index position 0.
  7. Print all the elements in the ArrayList.

Answer:

import java.util.ArrayList;

public class ArrayListOperations {
    
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(5);
        numbers.add(10);
        numbers.add(7);
        numbers.add(3);
        numbers.add(9);
        
        // 1. Print the number of elements in the ArrayList.
        System.out.println("Number of elements: " + numbers.size());
        
        // 2. Check if the ArrayList is empty.
        System.out.println("Is the ArrayList empty? " + numbers.isEmpty());
        
        // 3. Retrieve and print the element at index position 3.
        int elementAtIndex3 = numbers.get(3);
        System.out.println("Element at index 3: " + elementAtIndex3);
        
        // 4. Replace the element at index position 1 with a new value of 15.
        numbers.set(1, 15);
        
        // 5. Remove the element at index position 2.
        numbers.remove(2);
        
        // 6. Add a new element with the value 12 at index position 0.
        numbers.add(0, 12);
        
        // 7. Print all the elements in the ArrayList.
        System.out.println("Elements in the ArrayList: " + numbers);
    }
}

Explanation:

  1. The size() method is used to get the number of elements in the ArrayList. In this case, it returns 5 as there are initially 5 elements in the numbers ArrayList.

  2. The isEmpty() method is used to check if the ArrayList is empty. In this case, it returns false as the numbers ArrayList is not empty.

  3. The get(int index) method is used to retrieve the element at the specified index. In this case, it retrieves the element at index 3, which is 3.

  4. The set(int index, E element) method is used to replace the element at the specified index with a new value. In this case, it replaces the element at index 1 with the value 15.

  5. The remove(int index) method is used to remove the element at the specified index from the ArrayList. In this case, it removes the element at index 2.

  6. The add(int index, E element) method is used to add a new element at the specified index in the ArrayList. In this case, it adds the element with the value 12 at index 0, shifting all other elements to the right.

  7. Printing the ArrayList using the toString() method displays all the elements in the ArrayList.