ArrayList

Cards (25)

  • ArrayList
    an instance can serve the same purpose as an array, except that it can change in length while the program is running
  • Why use ArrayList and not Arrays
    Using an instance of ArrayList is less efficient than using an array.
  • An instance of ArrayList can store only objects; it cannot contain values of a primitive type, such as int, double, or char.
  • ArrayList<Base_Type>variable = new ArrayList< Base_Type >();
    *** invoke the default constructor - which is the size of 10
  • ArrayList< Base_Type > variable = new ArrayList< Base_Type >(20);
    *** initial capacity is 20, but can still change
  • ArrayList<Base_Type> variable = new ArrayList<>()
    ***starting with JDK 7 it will not require for the base type to be repeated
  • An object of ArrayList can be used like an array, but you must use methods instead of an array’s square-bracket notation.
  • Example:
    String[] anArray = new String[20];
    ArrayList<String> aList = new ArrayList<>(20);
    anArray[index] = "Hi
    Mom!"; //assign values
    aList.set(index, "Hi Mom!");
    //The method set is used to change the value of
    //existing elements, not to set them for the first time.
    String temp = anArray[index]; //get element
    String temp = aList.get(index);
    To set an element for the first time, you use the method add.
  • To set an element for the first time, you use the method add.
    This method adds elements at index positions 0, 1, 2, and so forth, in that order.
  • ArrayList:
    This method adds elements at index positions 0, 1, 2, and so forth, in that order.
    An ArrayList object must always be filled in this order.
  • ArrayList:
    The method add has two forms; that is, it is overloaded.
  • ArrayList:
    When given one argument, add adds an element immediately after the last currently occupied position.
  • ArrayList:
    Given two arguments, the method adds the element at the indicated position, assuming that the indicated position is not after an unoccupied position. 

    Þ All other elements will adjust position.
  • public Base_Type get(int index)
    Returns the element at the position specified by index.
  • public Base_Type set(int index, Base_Type element)
    Replaces the element at the position specified by index with the given element.
  • public Base_Type remove(int index)
    Removes and returns the element at the specified
    index.
  • public Base_Type remove(int index)
    Shifts elements at subsequent positions toward position index by decreasing their indices by 1.
  • public boolean remove(Object element)
    • Removes the first occurrence of element in this list, and shifts elements at subsequent positions toward the removed element by decreasing their indices by 1.
  • public boolean remove(Object element)
    Returns true if element was removed; otherwise returns false and does not alter the list.
  • public void clear()
    • Removes all elements from this list.
  • public boolean contains(Object element)
    • Returns true if element is in this list; otherwise, returns false.
  • public int indexOf(Object element)
    • Returns the index of the first occurrence of element in this list.
  • public int indexOf(Object element)
    Returns –1 if element is not on the list.
  • public boolean isEmpty() 

    • Returns true if this list is empty; otherwise, returns false
  • public int size()
    Returns the number of elements in this list.