Post

Created by @nathanedwards
 at November 5th 2023, 1:16:22 am.

Question:

Consider the following scenario:

You are working on a project that requires storing and manipulating a collection of data elements. After analyzing the project requirements, you decide to use an ArrayList data structure in Java. However, you realize that you need a deeper understanding of ArrayList and its functionalities.

You decide to write a program that demonstrates various operations on an ArrayList. Your task is to complete the program by filling in the missing code sections.

import java.util.ArrayList;

public class ArrayListDemo {

    public static void main(String[] args) {
        // Create an ArrayList of type String called "fruits"
        ArrayList<String> fruits = new ArrayList<>();
        
        // Step 1: Add the following fruits to the ArrayList: "apple", "banana", "orange"
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");
        
        // Step 2: Print the total number of fruits in the ArrayList
        System.out.println("Total number of fruits: " + fruits.size());
        
        // Step 3: Check if the ArrayList contains the fruit "banana" and print the result
        boolean containsBanana = fruits.contains("banana");
        System.out.println("Does the ArrayList contain 'banana'? " + containsBanana);
        
        // Step 4: Get the index of the fruit "orange" in the ArrayList and print the result
        int orangeIndex = fruits.indexOf("orange");
        System.out.println("Index of 'orange' in the ArrayList: " + orangeIndex);
        
        // Step 5: Remove the fruit at index 1 from the ArrayList
        fruits.remove(1);
        
        // Step 6: Print the updated ArrayList
        System.out.println("Updated ArrayList: " + fruits);
    }
}

In the given program, complete the missing code sections marked as "Step 1", "Step 2", "Step 3", "Step 4", "Step 5", and "Step 6" to perform the required operations on the ArrayList. Run the program and observe the output.

Answer:

import java.util.ArrayList;

public class ArrayListDemo {

    public static void main(String[] args) {
        // Create an ArrayList of type String called "fruits"
        ArrayList<String> fruits = new ArrayList<>();
        
        // Step 1: Add the following fruits to the ArrayList: "apple", "banana", "orange"
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");
        
        // Step 2: Print the total number of fruits in the ArrayList
        System.out.println("Total number of fruits: " + fruits.size());
        
        // Step 3: Check if the ArrayList contains the fruit "banana" and print the result
        boolean containsBanana = fruits.contains("banana");
        System.out.println("Does the ArrayList contain 'banana'? " + containsBanana);
        
        // Step 4: Get the index of the fruit "orange" in the ArrayList and print the result
        int orangeIndex = fruits.indexOf("orange");
        System.out.println("Index of 'orange' in the ArrayList: " + orangeIndex);
        
        // Step 5: Remove the fruit at index 1 from the ArrayList
        fruits.remove(1);
        
        // Step 6: Print the updated ArrayList
        System.out.println("Updated ArrayList: " + fruits);
    }
}

Output:

Total number of fruits: 3
Does the ArrayList contain 'banana'? true
Index of 'orange' in the ArrayList: 2
Updated ArrayList: [apple, orange]

Explanation:

Step 1: To add fruits to the ArrayList, we use the add() method. In this case, we add three fruits: "apple", "banana", and "orange".

Step 2: To get the total number of fruits in the ArrayList, we use the size() method. It returns the number of elements in the ArrayList.

Step 3: To check if the ArrayList contains a specific fruit, we use the contains() method. It returns true if the fruit is present in the ArrayList, otherwise false.

Step 4: To get the index of a specific fruit in the ArrayList, we use the indexOf() method. It returns the first occurrence of the fruit's index in the ArrayList. If the fruit is not found, it returns -1.

Step 5: To remove a fruit from the ArrayList, we use the remove() method. We specify the index of the fruit that we want to remove.

Step 6: To print the updated ArrayList, we use the toString() method implicitly as part of the concatenation. It returns a string representation of the ArrayList.

Upon running the program, it will output:

Total number of fruits: 3
Does the ArrayList contain 'banana'? true
Index of 'orange' in the ArrayList: 2
Updated ArrayList: [apple, orange]

This demonstrates the basic operations that can be performed on an ArrayList in Java.