Class IntArrayList

All Implemented Interfaces:
IntCollection, IntList, Serializable, Cloneable, Iterable<Integer>, Collection<Integer>, List<Integer>, RandomAccess
Direct Known Subclasses:
SortedIntArrayList

public class IntArrayList extends AbstractList<Integer> implements IntList, RandomAccess, Cloneable, Serializable
An ArrayList that stores things using int[] instead of Object[]. null values are not supported.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int[]
    The array buffer into which the elements of the ArrayList are stored.
    protected int
    The size of the ArrayList (the number of elements it contains).

    Fields inherited from class java.util.AbstractList

    modCount
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty list with an initial capacity of ten.
    IntArrayList(int initialCapacity)
    Constructs an empty list with the specified initial capacity.
    IntArrayList(int[] elements)
     
    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(int o)
    Appends the specified element to the end of this list.
    void
    add(int index, int element)
    Inserts the specified element at the specified position in this list.
    void
    add(int index, Integer element)
    Inserts the specified element at the specified position in this list.
    boolean
    Appends the specified element to the end of this list.
    boolean
    addAll(int index, Collection<? extends Integer> c)
    Inserts all of the elements in the specified Collection into this list, starting at the specified position.
    boolean
    addAll(Collection<? extends Integer> c)
    Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.
    void
    Removes all of the elements from this list.
    Returns a shallow copy of this IntArrayList instance.
    boolean
    contains(int elem)
    Returns true if this list contains the specified element.
    boolean
    Returns true if this list contains the specified element.
    void
    ensureCapacity(int minCapacity)
    Increases the capacity of this IntArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
    get(int index)
    Returns the element at the specified position in this list.
    int
    getInt(int index)
    Returns the element at the specified position in this list.
    int
    indexOf(int elem)
    Searches for the first occurrence of the given argument, testing for equality using the equals method.
    int
    Searches for the first occurrence of the given argument, testing for equality using the equals method.
    boolean
    Tests if this list has no elements.
    int
    lastIndexOf(int elem)
    Returns the index of the last occurrence of the specified object in this list.
    int
    Returns the index of the last occurrence of the specified object in this list.
    remove(int index)
    Removes the element at the specified position in this list.
    boolean
    Removes a single instance of the specified element from this list, if it is present (optional operation).
    int
    removeAtIndex(int index)
    Removes the element at the specified position in this list.
    boolean
    removeByValue(int value)
    Removes a single instance of the specified element from this list, if it is present (optional operation).
    protected void
    removeRange(int fromIndex, int toIndex)
    Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
    int
    set(int index, int element)
    Replaces the element at the specified position in this list with the specified element.
    set(int index, Integer element)
    Replaces the element at the specified position in this list with the specified element.
    int
    Returns the number of elements in this list.
    Returns an array containing all of the elements in this list in the correct order.
    <T> T[]
    toArray(T[] a)
    Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.
    int[]
    Returns an array containing all of the elements in this list in the correct order.
    void
    Trims the capacity of this IntArrayList instance to be the list's current size.

    Methods inherited from class java.util.AbstractList

    equals, hashCode, iterator, listIterator, listIterator, subList

    Methods inherited from class java.util.AbstractCollection

    containsAll, removeAll, retainAll, toString

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.Collection

    parallelStream, removeIf, stream, toArray

    Methods inherited from interface java.lang.Iterable

    forEach
  • Field Details

    • elementData

      protected transient int[] elementData
      The array buffer into which the elements of the ArrayList are stored. The capacity of the ArrayList is the length of this array buffer.
    • size

      protected int size
      The size of the ArrayList (the number of elements it contains).
  • Constructor Details

    • IntArrayList

      public IntArrayList(int initialCapacity)
      Constructs an empty list with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity of the list.
      Throws:
      IllegalArgumentException - if the specified initial capacity is negative
    • IntArrayList

      public IntArrayList()
      Constructs an empty list with an initial capacity of ten.
    • IntArrayList

      public IntArrayList(Collection<? extends Number> c)
      Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The IntArrayList instance has an initial capacity of 110% the size of the specified collection.
      Parameters:
      c - the collection whose elements are to be placed into this list.
      Throws:
      NullPointerException - if the specified collection is null.
    • IntArrayList

      public IntArrayList(int[] elements)
  • Method Details

    • trimToSize

      public void trimToSize()
      Trims the capacity of this IntArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an IntArrayList instance.
    • ensureCapacity

      public void ensureCapacity(int minCapacity)
      Increases the capacity of this IntArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
      Parameters:
      minCapacity - the desired minimum capacity.
    • size

      public int size()
      Returns the number of elements in this list.
      Specified by:
      size in interface Collection<Integer>
      Specified by:
      size in interface List<Integer>
      Specified by:
      size in class AbstractCollection<Integer>
      Returns:
      the number of elements in this list.
    • isEmpty

      public boolean isEmpty()
      Tests if this list has no elements.
      Specified by:
      isEmpty in interface Collection<Integer>
      Specified by:
      isEmpty in interface List<Integer>
      Overrides:
      isEmpty in class AbstractCollection<Integer>
      Returns:
      true if this list has no elements; false otherwise.
    • contains

      public boolean contains(Object elem)
      Returns true if this list contains the specified element.
      Specified by:
      contains in interface Collection<Integer>
      Specified by:
      contains in interface List<Integer>
      Overrides:
      contains in class AbstractCollection<Integer>
      Parameters:
      elem - element whose presence in this List is to be tested.
      Returns:
      true if the specified element is present; false otherwise.
    • contains

      public boolean contains(int elem)
      Returns true if this list contains the specified element.
      Specified by:
      contains in interface IntCollection
      Parameters:
      elem - element whose presence in this List is to be tested.
      Returns:
      true if the specified element is present; false otherwise.
    • indexOf

      public int indexOf(Object elem)
      Searches for the first occurrence of the given argument, testing for equality using the equals method.
      Specified by:
      indexOf in interface List<Integer>
      Overrides:
      indexOf in class AbstractList<Integer>
      Parameters:
      elem - an object.
      Returns:
      the index of the first occurrence of the argument in this list; returns -1 if the object is not found.
      See Also:
    • indexOf

      public int indexOf(int elem)
      Searches for the first occurrence of the given argument, testing for equality using the equals method.
      Specified by:
      indexOf in interface IntList
      Parameters:
      elem - an object.
      Returns:
      the index of the first occurrence of the argument in this list; returns -1 if the object is not found.
      See Also:
    • lastIndexOf

      public int lastIndexOf(Object elem)
      Returns the index of the last occurrence of the specified object in this list.
      Specified by:
      lastIndexOf in interface List<Integer>
      Overrides:
      lastIndexOf in class AbstractList<Integer>
      Parameters:
      elem - the desired element.
      Returns:
      the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.
    • lastIndexOf

      public int lastIndexOf(int elem)
      Returns the index of the last occurrence of the specified object in this list.
      Specified by:
      lastIndexOf in interface IntList
      Parameters:
      elem - the desired element.
      Returns:
      the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.
    • clone

      public Object clone()
      Returns a shallow copy of this IntArrayList instance. (The elements themselves are not copied.)
      Overrides:
      clone in class Object
      Returns:
      a clone of this IntArrayList instance.
    • toArray

      public Object[] toArray()
      Returns an array containing all of the elements in this list in the correct order.
      Specified by:
      toArray in interface Collection<Integer>
      Specified by:
      toArray in interface List<Integer>
      Overrides:
      toArray in class AbstractCollection<Integer>
      Returns:
      an array containing all of the elements in this list in the correct order.
    • toArrayInt

      public int[] toArrayInt()
      Returns an array containing all of the elements in this list in the correct order.
      Specified by:
      toArrayInt in interface IntCollection
      Returns:
      an array containing all of the elements in this list in the correct order.
    • toArray

      public <T> T[] toArray(T[] a)
      Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

      If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.

      Specified by:
      toArray in interface Collection<Integer>
      Specified by:
      toArray in interface List<Integer>
      Overrides:
      toArray in class AbstractCollection<Integer>
      Parameters:
      a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
      Returns:
      an array containing the elements of the list.
      Throws:
      ArrayStoreException - if the runtime type of a is not a supertype of the runtime type of every element in this list.
    • get

      public Integer get(int index)
      Returns the element at the specified position in this list.
      Specified by:
      get in interface List<Integer>
      Specified by:
      get in class AbstractList<Integer>
      Parameters:
      index - index of element to return.
      Returns:
      the element at the specified position in this list.
      Throws:
      IndexOutOfBoundsException - if index is out of range (index < 0 || index >= size()).
    • getInt

      public int getInt(int index)
      Returns the element at the specified position in this list.
      Specified by:
      getInt in interface IntList
      Parameters:
      index - index of element to return.
      Returns:
      the element at the specified position in this list.
      Throws:
      IndexOutOfBoundsException - if index is out of range (index < 0 || index >= size()).
    • set

      public Integer set(int index, Integer element)
      Replaces the element at the specified position in this list with the specified element.
      Specified by:
      set in interface List<Integer>
      Overrides:
      set in class AbstractList<Integer>
      Parameters:
      index - index of element to replace.
      element - element to be stored at the specified position.
      Returns:
      the element previously at the specified position.
      Throws:
      IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).
    • set

      public int set(int index, int element)
      Replaces the element at the specified position in this list with the specified element.
      Specified by:
      set in interface IntList
      Parameters:
      index - index of element to replace.
      element - element to be stored at the specified position.
      Returns:
      the element previously at the specified position.
      Throws:
      IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).
    • add

      public boolean add(Integer o)
      Appends the specified element to the end of this list.
      Specified by:
      add in interface Collection<Integer>
      Specified by:
      add in interface List<Integer>
      Overrides:
      add in class AbstractList<Integer>
      Parameters:
      o - element to be appended to this list.
      Returns:
      true (as per the general contract of Collection.add).
    • add

      public boolean add(int o)
      Appends the specified element to the end of this list.
      Specified by:
      add in interface IntCollection
      Parameters:
      o - element to be appended to this list.
      Returns:
      true (as per the general contract of Collection.add).
    • add

      public void add(int index, Integer element)
      Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
      Specified by:
      add in interface List<Integer>
      Overrides:
      add in class AbstractList<Integer>
      Parameters:
      index - index at which the specified element is to be inserted.
      element - element to be inserted.
      Throws:
      IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).
    • add

      public void add(int index, int element)
      Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
      Specified by:
      add in interface IntList
      Parameters:
      index - index at which the specified element is to be inserted.
      element - element to be inserted.
      Throws:
      IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).
    • remove

      public Integer remove(int index)
      Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
      Specified by:
      remove in interface List<Integer>
      Overrides:
      remove in class AbstractList<Integer>
      Parameters:
      index - the index of the element to removed.
      Returns:
      the element that was removed from the list.
      Throws:
      IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).
    • removeAtIndex

      public int removeAtIndex(int index)
      Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
      Specified by:
      removeAtIndex in interface IntList
      Parameters:
      index - the index of the element to removed.
      Returns:
      the element that was removed from the list.
      Throws:
      IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).
    • remove

      public boolean remove(Object o)
      Removes a single instance of the specified element from this list, if it is present (optional operation). More formally, removes an element e such that (o == null ? e == null : o.equals(e)), if the list contains one or more such elements. Returns true if the list contained the specified element (or equivalently, if the list changed as a result of the call).
      Specified by:
      remove in interface Collection<Integer>
      Specified by:
      remove in interface List<Integer>
      Overrides:
      remove in class AbstractCollection<Integer>
      Parameters:
      o - element to be removed from this list, if present.
      Returns:
      true if the list contained the specified element.
    • removeByValue

      public boolean removeByValue(int value)
      Removes a single instance of the specified element from this list, if it is present (optional operation). More formally, removes an element e such that (o == null ? e == null : o.equals(e)), if the list contains one or more such elements. Returns true if the list contained the specified element (or equivalently, if the list changed as a result of the call).
      Specified by:
      removeByValue in interface IntCollection
      Parameters:
      value - element to be removed from this list, if present.
      Returns:
      true if the list contained the specified element.
    • clear

      public void clear()
      Removes all of the elements from this list. The list will be empty after this call returns.
      Specified by:
      clear in interface Collection<Integer>
      Specified by:
      clear in interface List<Integer>
      Overrides:
      clear in class AbstractList<Integer>
    • addAll

      public boolean addAll(Collection<? extends Integer> c)
      Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)
      Specified by:
      addAll in interface Collection<Integer>
      Specified by:
      addAll in interface List<Integer>
      Overrides:
      addAll in class AbstractCollection<Integer>
      Parameters:
      c - the elements to be inserted into this list.
      Returns:
      true if this list changed as a result of the call.
      Throws:
      NullPointerException - if the specified collection is null.
    • addAll

      public boolean addAll(int index, Collection<? extends Integer> c)
      Inserts all of the elements in the specified Collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified Collection's iterator.
      Specified by:
      addAll in interface List<Integer>
      Overrides:
      addAll in class AbstractList<Integer>
      Parameters:
      index - index at which to insert first element from the specified collection.
      c - elements to be inserted into this list.
      Returns:
      true if this list changed as a result of the call.
      Throws:
      IndexOutOfBoundsException - if index out of range (index < 0 || index > size()).
      NullPointerException - if the specified Collection is null.
    • removeRange

      protected void removeRange(int fromIndex, int toIndex)
      Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex == fromIndex, this operation has no effect.)
      Overrides:
      removeRange in class AbstractList<Integer>
      Parameters:
      fromIndex - index of first element to be removed.
      toIndex - index after last element to be removed.