Class IntArrayList

java.lang.Object
java.util.AbstractCollection<Integer>
java.util.AbstractList<Integer>
org.jhotdraw8.collection.primitive.IntArrayList
All Implemented Interfaces:
Iterable<Integer>, Collection<Integer>, List<Integer>, SequencedCollection<Integer>, IntList, IntSequencedCollection

public class IntArrayList extends AbstractList<Integer> implements IntList
A int-valued list backed by a primitive array.
Author:
Werner Randelshofer
  • Constructor Details

    • IntArrayList

      public IntArrayList()
      Creates a new empty instance with 0 initial capacity.
    • IntArrayList

      public IntArrayList(int initialCapacity)
      Creates a new empty instance with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity
    • IntArrayList

      public IntArrayList(Collection<Integer> collection)
      Creates a new instance from the specified collection
      Parameters:
      collection - a collection of integers
  • Method Details

    • of

      public static IntArrayList of(int... items)
      Creates a new instance with the specified items.
      Parameters:
      items - the items (the newly created instance references the provided array)
      Returns:
      the new instance
    • add

      public boolean add(Integer integer)
      Specified by:
      add in interface Collection<Integer>
      Specified by:
      add in interface IntList
      Specified by:
      add in interface List<Integer>
      Overrides:
      add in class AbstractList<Integer>
    • addAllAsInt

      public void addAllAsInt(IntArrayList that)
      Adds all items of the specified list to this list.
      Parameters:
      that - another list
    • addAllInto

      public <T extends Collection<Integer>> T addAllInto(T out)
      Adds all items of this collection to the specified collection.
      Type Parameters:
      T - the type of the collection
      Parameters:
      out - the output collection
      Returns:
      out
    • addAsInt

      public void addAsInt(int newItem)
      Adds a new item to the end of the list.
      Specified by:
      addAsInt in interface IntList
      Parameters:
      newItem - the new item
      See Also:
    • addAsInt

      public void addAsInt(int index, int newItem)
      Inserts a new item at the specified index into this list.
      Specified by:
      addAsInt in interface IntList
      Parameters:
      index - the index
      newItem - the new item
      See Also:
    • clear

      public void clear()
      Clears the list in O(1).
      Specified by:
      clear in interface Collection<Integer>
      Specified by:
      clear in interface List<Integer>
      Overrides:
      clear in class AbstractList<Integer>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<Integer>
      Specified by:
      contains in interface List<Integer>
      Overrides:
      contains in class AbstractCollection<Integer>
    • copyInto

      public void copyInto(int[] a, int offset)
      Copies the contents of this list into the provided array.
      Parameters:
      a - an array
      offset - the offset into the array
    • equals

      public boolean equals(@Nullable Object obj)
      Specified by:
      equals in interface Collection<Integer>
      Specified by:
      equals in interface List<Integer>
      Overrides:
      equals in class AbstractList<Integer>
    • get

      public Integer get(int index)
      Specified by:
      get in interface List<Integer>
      Specified by:
      get in class AbstractList<Integer>
    • getArray

      public int[] getArray()
      Gets the array that is used internally by this list.
      Returns:
      the internal array
    • getAsInt

      public int getAsInt(int index)
      Gets the item at the specified index.
      Specified by:
      getAsInt in interface IntList
      Parameters:
      index - an index
      Returns:
      the item at the index
      See Also:
    • getFirstAsInt

      public int getFirstAsInt()
      Specified by:
      getFirstAsInt in interface IntSequencedCollection
    • getLastAsInt

      public int getLastAsInt()
      Specified by:
      getLastAsInt in interface IntSequencedCollection
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<Integer>
      Specified by:
      hashCode in interface List<Integer>
      Overrides:
      hashCode in class AbstractList<Integer>
    • indexOfAsInt

      public int indexOfAsInt(int item)
      Specified by:
      indexOfAsInt in interface IntList
      See Also:
    • indexOfAsInt

      public int indexOfAsInt(int item, int start)
    • intStream

      public IntStream intStream()
      Returns a stream for processing the items of this list.
      Returns:
      a stream
    • isEmpty

      public boolean isEmpty()
      Returns true if size==0.
      Specified by:
      isEmpty in interface Collection<Integer>
      Specified by:
      isEmpty in interface IntList
      Specified by:
      isEmpty in interface List<Integer>
      Overrides:
      isEmpty in class AbstractCollection<Integer>
      Returns:
      true if empty
    • iterator

      public PrimitiveIterator.OfInt iterator()
      Returns an iterator for this list.
      Specified by:
      iterator in interface Collection<Integer>
      Specified by:
      iterator in interface Iterable<Integer>
      Specified by:
      iterator in interface List<Integer>
      Overrides:
      iterator in class AbstractList<Integer>
      Returns:
      an iterator over the elements of this list
    • lastIndexOfAsInt

      public int lastIndexOfAsInt(int item)
      Specified by:
      lastIndexOfAsInt in interface IntList
      See Also:
    • lastIndexOfAsInt

      public int lastIndexOfAsInt(int item, int start)
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<Integer>
      Specified by:
      remove in interface List<Integer>
      Overrides:
      remove in class AbstractCollection<Integer>
    • removeAtAsInt

      public int removeAtAsInt(int index)
      Removes the item at the specified index from this list.
      Specified by:
      removeAtAsInt in interface IntList
      Parameters:
      index - an index
      Returns:
      the removed item
    • removeIfAsInt

      public boolean removeIfAsInt(IntPredicate filter)
      Removes all the elements of this collection that satisfy the given predicate.
      Parameters:
      filter - a predicate which returns true for elements to be removed
      Returns:
      true if any elements were removed
    • removeLastAsInt

      public int removeLastAsInt()
      Removes the last item
      Specified by:
      removeLastAsInt in interface IntSequencedCollection
      Returns:
      the removed item
      Throws:
      NoSuchElementException - if the list is empty
    • reversed

      public List<Integer> reversed()
      Specified by:
      reversed in interface List<Integer>
      Specified by:
      reversed in interface SequencedCollection<Integer>
    • set

      public Integer set(int index, Integer newItem)
      Replaces the item at the specified index.
      Specified by:
      set in interface List<Integer>
      Overrides:
      set in class AbstractList<Integer>
      Parameters:
      index - an index
      newItem - the new item
      Returns:
      the old item
    • setAsInt

      public int setAsInt(int index, int newItem)
      Replaces the item at the specified index.
      Parameters:
      index - an index
      newItem - the new item
      Returns:
      the old item
    • setSize

      public void setSize(int newSize)
      Sets the size of this list. If the new size is greater than the current size, new 0 items are added to the end of the list. If the new size is less than the current size, all items at indices greater or equal newSize are discarded.
      Parameters:
      newSize - the new size
    • size

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

      public void sort()
      Sorts the items in ascending order.
    • sort

      public void sort(@Nullable Comparator<? super Integer> c)
      Sorts this list according to the order induced by the specified Comparator. The sort is stable: it does not reorder equal elements.
      Specified by:
      sort in interface List<Integer>
      Parameters:
      c - the Comparator used to compare list elements. A null value indicates that the elements' natural ordering should be used.
    • spliterator

      public Spliterator.OfInt spliterator()
      Returns a spliterator for this list.
      Specified by:
      spliterator in interface Collection<Integer>
      Specified by:
      spliterator in interface Iterable<Integer>
      Specified by:
      spliterator in interface List<Integer>
      Returns:
      a spliterator over the elements of this list
    • toIntArray

      public int[] toIntArray()
      Returns a new array containing all the elements in this collection.
      Returns:
      array
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<Integer>
    • trimToSize

      public void trimToSize()
      Trims the capacity of the list its current size.