7.2 ArrayList Methods

Cards (43)

  • The `get(index)` method modifies the `ArrayList`.
    False
  • The `size()` method returns the number of elements in an `ArrayList`.

    True
  • Arrange the following steps to add an element to the end of an `ArrayList` using `add(element)`:
    1️⃣ Specify the `ArrayList` name
    2️⃣ Call the `add(element)` method
    3️⃣ Pass the element to be added
  • What is the primary function of the `get(index)` method in an `ArrayList`?
    Retrieves element at index
  • The `remove(index)` method shifts subsequent elements to the left.

    True
  • Match the `ArrayList` method with its description:
    `remove(element)` ↔️ Removes the first occurrence of the specified element
    `remove(index)` ↔️ Removes the element at the specified index
    `add(element)` ↔️ Adds an element to the end of the list
    `get(index)` ↔️ Retrieves the element at the specified index
  • What does the `add(element)` method do to an `ArrayList`?
    Adds an element to the end
  • What is the purpose of the `remove(index)` method in an `ArrayList`?
    Removes the element at index
  • Which method checks if an `ArrayList` contains a specific element?
    contains(element)
  • What is the key difference between `add(element)` and `add(index, element)` in an `ArrayList`?
    `add(index, element)` inserts at index
  • The `get(index)` method does not modify the `ArrayList`, it only retrieves the element at the specified index
  • The `remove(index)` method is used to remove the element at the specified index
  • The `add(element)` method appends the specified element to the end of the list
  • The `set(index, element)` method replaces the element at the specified index
  • The `isEmpty()` method in `ArrayList` returns true if the list is empty.
  • The `set(index, element)` method in `ArrayList` replaces the existing element at a specified index.
  • The `isEmpty()` method in `ArrayList` returns `true` if the list is empty
  • What does the `contains(element)` method in `ArrayList` check?
    Existence of an element
  • What does the `indexOf()` method in `ArrayList` return if the element is not found?
    -1
  • The `lastIndexOf(element)` method returns -1 if the specified element is not found in the `ArrayList`.

    True
  • In the example code, `groceryList.lastIndexOf("Apples")` returns the index 2
  • The `add(index, element)` method inserts an element at a specified index
  • The `set(index, element)` method replaces the element at a specified index
  • The `indexOf(element)` method returns the first index of the specified element
  • The `add(index, element)` method can increase the size of the `ArrayList`.

    True
  • Arrange the following steps to retrieve an element from an `ArrayList` using `get(index)`:
    1️⃣ Specify the `ArrayList` name
    2️⃣ Call the `get(index)` method
    3️⃣ Pass the index of the element to retrieve
  • The `remove(element)` method removes the first occurrence of a specified element from an `ArrayList` and shifts subsequent elements to the left.

    True
  • The `add(index, element)` method inserts an element at a specified index, shifting subsequent elements to the right.

    True
  • The `size()` method returns the number of elements in an `ArrayList`.

    True
  • The `add(index, element)` method inserts an element at a specific index in the `ArrayList`.
  • The `size()` method in `ArrayList` returns the number of elements currently stored in the list.

    True
  • What is the purpose of the `isEmpty()` method in `ArrayList` when used with other methods like `add()` and `get()`?
    Robust and defensive code
  • The `contains(element)` method checks if the specified element exists in the `ArrayList` using the syntax `list.contains(element);`
  • The `indexOf()` method returns an integer representing the index
  • The `lastIndexOf(element)` method returns the index of the last occurrence
  • Match the `ArrayList` method with its primary purpose:
    `get(index)` ↔️ Retrieves an element
    `remove(element)` ↔️ Removes the first occurrence of an element
    `add(index, element)` ↔️ Inserts an element at a specified index
    `add(element)` ↔️ Adds an element to the end
  • The `isEmpty()` method in `ArrayList` returns `true` if the list is empty
  • The `remove(element)` method removes the first occurrence of a specified element from the `ArrayList`.
    True
  • Using `isEmpty()` with other `ArrayList` methods ensures more robust and defensive code.
  • The `isEmpty()` method returns `false` if the `ArrayList` contains one or more elements.

    True