7.1 Introduction to ArrayList

Cards (44)

  • An ArrayList automatically resizes when elements are added or removed.

    True
  • The size of an ArrayList is resizable
  • What happens to the size of an ArrayList when you add or remove elements?
    It grows or shrinks
  • ArrayLists require the use of wrapper classes for primitive data types.

    True
  • What is one key benefit of using an ArrayList over a regular array in Java?
    Dynamically resizable
  • What package must be imported to use ArrayList in Java?
    java.util.ArrayListjava.util.ArrayList
  • The `remove()` method in an ArrayList shifts subsequent elements to the left.

    True
  • Steps to iterate through an ArrayList using a regular `for` loop
    1️⃣ Initialize index variable
    2️⃣ Check loop condition (index < size())
    3️⃣ Retrieve element at current index
    4️⃣ Process the element
    5️⃣ Increment index variable
  • An ArrayList is a dynamic, ordered collection of objects
  • Match the feature with the corresponding data structure:
    Data Type | ArrayList ↔️ Objects only
    Size | ArrayList ↔️ Resizable
    Data Type | Array ↔️ Primitive and Objects
    Size | Array ↔️ Fixed
  • What is the main advantage of using ArrayList over a regular array?
    Dynamic size
  • ArrayLists can only store objects, requiring primitive types to be wrapped.
  • What type of data can an ArrayList store?
    Objects only
  • To use ArrayList in Java, you must import the java.util.ArrayList package.
  • Order the steps to create and use an ArrayList:
    1️⃣ Import java.util.ArrayList
    2️⃣ Create an ArrayList object
    3️⃣ Add elements using add()
    4️⃣ Access elements using get()
  • ArrayLists avoid the need to predefine a fixed size.
    True
  • The size of an ArrayList is resizable, allowing it to adapt to changing data requirements.
  • The dynamic size of ArrayLists avoids the need to predefine a fixed size.
  • ArrayLists can store primitive data types directly without wrapping them into objects.
    False
  • What does the `add()` method do in an ArrayList?
    Adds an element
  • What does the `size()` method return in an ArrayList?
    Number of elements
  • What is the main difference between an ArrayList and a regular array in terms of size?
    Resizable vs. fixed
  • What type of data can an ArrayList store in Java?
    Objects only
  • The main advantage of using an ArrayList over an array is its dynamic size
  • What happens to the size of an ArrayList when new elements are added?
    It is dynamically resized
  • Match the ArrayList method with its description:
    `add(element)` ↔️ Adds the element to the end
    `remove(index)` ↔️ Removes the element at index
    `get(index)` ↔️ Returns the element at index
    `size()` ↔️ Returns the number of elements
  • When should you use an enhanced `for` loop instead of a regular `for` loop when iterating through an ArrayList?
    When no index is needed
  • To handle a NullPointerException, you must ensure the ArrayList is initialized before use.

    True
  • ArrayLists store only objects, while arrays can store primitives and objects.
  • The size of an ArrayList is fixed once initialized.
    False
  • What is the primary use case for an ArrayList when compared to an array?
    Managing data size
  • What happens to subsequent elements in an ArrayList when an element is removed using `remove(index)`?
    They shift to the left
  • An IndexOutOfBoundsException occurs when accessing an index outside the range of the ArrayList
  • What is a key advantage of using ArrayList over regular arrays in terms of size?
    Dynamic resizing
  • The size of an ArrayList is dynamically resizable
  • ArrayLists are particularly useful when the size of the data is not known in advance
  • The `get()` method returns the element at the specified index
  • The enhanced `for` loop is more concise than a regular `for` loop for iterating through an ArrayList.

    True
  • ArrayLists automatically resize when elements are added or removed, unlike arrays.
    True
  • What import is required to use ArrayList in Java?
    `java.util.ArrayList`