Add “Cairo” to the end of the travelDestinations array

Add “Cairo” to the end of the travelDestinations array.

Add “Mexico City” to the front of the travelDestinations array.

Update arrLen with the new length of the travelDestinations after completing the two previous problems. in java

The correct answer and explanation is:

Here’s how to solve the given task in Java, along with a detailed explanation:

Java Code:

import java.util.ArrayList;

public class TravelDestinations {
    public static void main(String[] args) {
        // Initializing the travelDestinations array list with some initial cities
        ArrayList<String> travelDestinations = new ArrayList<>();
        travelDestinations.add("New York");
        travelDestinations.add("Tokyo");
        travelDestinations.add("Paris");

        // Add "Cairo" to the end of the array
        travelDestinations.add("Cairo");

        // Add "Mexico City" to the front of the array
        travelDestinations.add(0, "Mexico City");

        // Update the arrLen with the new length of the travelDestinations
        int arrLen = travelDestinations.size();

        // Print the modified list and the new length
        System.out.println("Updated Travel Destinations: " + travelDestinations);
        System.out.println("Length of Travel Destinations: " + arrLen);
    }
}

Explanation:

  1. ArrayList Initialization:
    • We begin by initializing an ArrayList called travelDestinations. In Java, ArrayList is a class in the java.util package that allows dynamic resizing. Unlike arrays, ArrayList can grow or shrink as needed, making it ideal for situations where the number of elements is uncertain.
  2. Adding “Cairo” to the End:
    • We use the add() method to add "Cairo" to the end of the list. In an ArrayList, the add() method appends the element to the end of the list. After this operation, "Cairo" will be the last element of the travelDestinations list.
  3. Adding “Mexico City” to the Front:
    • We use the add(index, element) method to insert "Mexico City" at the front of the list. The add(0, "Mexico City") method specifically inserts "Mexico City" at index 0, shifting all other elements one position to the right. This ensures that "Mexico City" becomes the first element.
  4. Updating arrLen:
    • After modifying the list, we update the variable arrLen to reflect the current size of the travelDestinations list. The size() method of ArrayList returns the number of elements in the list. After the two insertions, the size will be updated accordingly.
  5. Output:
    • Finally, we print the updated travelDestinations list and the new size (arrLen), which will be 5 in this case. Initially, there were 3 cities in the list, and after adding two more cities, the list’s size becomes 5.

Output:

Updated Travel Destinations: [Mexico City, New York, Tokyo, Paris, Cairo]
Length of Travel Destinations: 5

Key Points:

  • ArrayList in Java is a part of the java.util package and provides dynamic arrays that can grow or shrink.
  • The add() method is used to append elements at the end of the list, and add(index, element) is used to insert elements at specific positions.
  • The size() method returns the number of elements in the list, and it is automatically updated after modifications to the list.
Scroll to Top