Class IntArrayList

All Implemented Interfaces:
Serializable, Cloneable, IntCollection, IntList

public class IntArrayList extends AbstractIntList implements Cloneable, Serializable
This class represents an array implemenation of lists of int values.
Since:
1.0
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default capacity of this list.
    static final int
    The default chunk size with which to increase the capacity of this list.
    static final double
    The default factor with which to increase the capacity of this list.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new array list with capacity 10 and a relative growth factor of 1.0.
    IntArrayList(int capacity)
    Creates a new array list with a specified capacity and a relative growth factor of 1.0.
    IntArrayList(int[] a)
    Creates a new array list with the same elements as a specified array.
    IntArrayList(int capacity, double growthFactor)
    Creates a new array list with a specified capacity and relative growth factor.
    IntArrayList(int capacity, int growthChunk)
    Creates a new array list with a specified capacity and absolute growth factor.
    Creates a new array list with the same elements as a specified collection.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int index, int v)
    Throws UnsupportedOperationException.
    int
    Returns the current capacity of this list.
    void
    Clears this collection.
    Returns a clone of this array list.
    boolean
    contains(int v)
    Indicates whether this collection contains a specified element.
    int
    ensureCapacity(int capacity)
    Ensures that this list has at least a specified capacity.
    boolean
    Indicates whether this collection is equal to some object.
    int
    get(int index)
    Returns the element at a specified position in this list.
    int
    Returns a hash code value for this collection.
    int
    indexOf(int c)
    Returns the index of the first occurance of a specified element in this list.
    int
    indexOf(int index, int c)
    Returns the index of the first occurance of a specified element in this list after or at a specified index.
    boolean
    Indicates whether this collection is empty.
    int
    lastIndexOf(int c)
    Returns the index of the last occurance of a specified element in this list.
    boolean
    remove(int v)
    Removes a specified element from this collection.
    int
    removeElementAt(int index)
    Throws UnsupportedOperationException.
    int
    set(int index, int v)
    Sets a specified element to a new value.
    int
    Returns the number of elements in this collection.
    int[]
    Returns the elements of this collection as an array.
    int[]
    toArray(int[] a)
    Returns the elements of this collection as an array.
    void
    Minimizes the memory used by this array list.

    Methods inherited from class zombie.util.list.AbstractIntList

    add, addAll, iterator, lastIndexOf, listIterator, listIterator

    Methods inherited from class zombie.util.AbstractIntCollection

    addAll, containsAll, removeAll, retainAll, toString

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface zombie.util.IntCollection

    addAll, containsAll, removeAll, retainAll
  • Field Details

    • DEFAULT_GROWTH_FACTOR

      public static final double DEFAULT_GROWTH_FACTOR
      The default factor with which to increase the capacity of this list.
      See Also:
    • DEFAULT_GROWTH_CHUNK

      public static final int DEFAULT_GROWTH_CHUNK
      The default chunk size with which to increase the capacity of this list.
      See Also:
    • DEFAULT_CAPACITY

      public static final int DEFAULT_CAPACITY
      The default capacity of this list.
      See Also:
  • Constructor Details

    • IntArrayList

      public IntArrayList()
      Creates a new array list with capacity 10 and a relative growth factor of 1.0.
      See Also:
    • IntArrayList

      public IntArrayList(IntCollection c)
      Creates a new array list with the same elements as a specified collection. The elements of the specified collection are added to the end of the list in the collection's iteration order.
      Parameters:
      c - the collection whose elements to add to the new list.
      Throws:
      NullPointerException - if c is null.
    • IntArrayList

      public IntArrayList(int[] a)
      Creates a new array list with the same elements as a specified array. The elements of the specified array are added to the end of the list in order of the array.
      Parameters:
      a - the array whose elements to add to the new list.
      Throws:
      NullPointerException - if a is null.
      Since:
      1.1
    • IntArrayList

      public IntArrayList(int capacity)
      Creates a new array list with a specified capacity and a relative growth factor of 1.0.
      Parameters:
      capacity - the initial capacity of the list.
      Throws:
      IllegalArgumentException - if capacity is negative.
      See Also:
    • IntArrayList

      public IntArrayList(int capacity, double growthFactor)
      Creates a new array list with a specified capacity and relative growth factor.

      The array capacity increases to capacity()*(1+growthFactor). This strategy is good for avoiding many capacity increases, but the amount of wasted memory is approximately the size of the list.

      Parameters:
      capacity - the initial capacity of the list.
      growthFactor - the relative amount with which to increase the the capacity when a capacity increase is needed.
      Throws:
      IllegalArgumentException - if capacity is negative; if growthFactor is negative.
    • IntArrayList

      public IntArrayList(int capacity, int growthChunk)
      Creates a new array list with a specified capacity and absolute growth factor.

      The array capacity increases to capacity()+growthChunk. This strategy is good for avoiding wasting memory. However, an overhead is potentially introduced by frequent capacity increases.

      Parameters:
      capacity - the initial capacity of the list.
      growthChunk - the absolute amount with which to increase the the capacity when a capacity increase is needed.
      Throws:
      IllegalArgumentException - if capacity is negative; if growthChunk is negative.
  • Method Details

    • ensureCapacity

      public int ensureCapacity(int capacity)
      Ensures that this list has at least a specified capacity. The actual capacity is calculated from the growth factor or growth chunk specified to the constructor.
      Parameters:
      capacity - the minimum capacity of this list.
      Returns:
      the new capacity of this list.
      See Also:
    • capacity

      public int capacity()
      Returns the current capacity of this list. The capacity is the number of elements that the list can contain without having to increase the amount of memory used.
      Returns:
      the current capacity of this list.
      See Also:
    • add

      public void add(int index, int v)
      Description copied from class: AbstractIntList
      Throws UnsupportedOperationException.
      Specified by:
      add in interface IntList
      Overrides:
      add in class AbstractIntList
      Parameters:
      index - the index at which to add the element. If index == size() the element is appended to this list.
      v - the int value to add to this list.
      See Also:
    • get

      public int get(int index)
      Description copied from interface: IntList
      Returns the element at a specified position in this list.
      Specified by:
      get in interface IntList
      Parameters:
      index - the position of the element to return.
      Returns:
      the element at the specified position.
    • set

      public int set(int index, int v)
      Description copied from interface: IntList
      Sets a specified element to a new value.
      Specified by:
      set in interface IntList
      Parameters:
      index - the index of the element whose value to set.
      v - the new value of the specified element.
      Returns:
      the previous value of the element.
    • removeElementAt

      public int removeElementAt(int index)
      Description copied from class: AbstractIntList
      Throws UnsupportedOperationException.
      Specified by:
      removeElementAt in interface IntList
      Overrides:
      removeElementAt in class AbstractIntList
      Parameters:
      index - the index of the element to remove.
      Returns:
      the value of the element removed.
    • trimToSize

      public void trimToSize()
      Minimizes the memory used by this array list. The underlying array is replaced by an array whose size is exactly the number of elements in this array list. The method can be used to free up memory after many removals.
      Specified by:
      trimToSize in interface IntCollection
      Overrides:
      trimToSize in class AbstractIntCollection
    • clone

      public Object clone()
      Returns a clone of this array list.
      Returns:
      a clone of this array list.
      Since:
      1.1
    • size

      public int size()
      Description copied from interface: IntCollection
      Returns the number of elements in this collection.
      Specified by:
      size in interface IntCollection
      Overrides:
      size in class AbstractIntCollection
      Returns:
      the number of elements in this collection.
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: IntCollection
      Indicates whether this collection is empty.
      Specified by:
      isEmpty in interface IntCollection
      Overrides:
      isEmpty in class AbstractIntCollection
      Returns:
      true if this collection is empty; returns false otherwise.
    • clear

      public void clear()
      Description copied from interface: IntCollection
      Clears this collection.
      Specified by:
      clear in interface IntCollection
      Overrides:
      clear in class AbstractIntCollection
    • contains

      public boolean contains(int v)
      Description copied from interface: IntCollection
      Indicates whether this collection contains a specified element.
      Specified by:
      contains in interface IntCollection
      Overrides:
      contains in class AbstractIntCollection
      Parameters:
      v - the element to test for containment.
      Returns:
      true if v is contained in this collection; returns false otherwise.
      See Also:
    • indexOf

      public int indexOf(int c)
      Description copied from interface: IntList
      Returns the index of the first occurance of a specified element in this list.
      Specified by:
      indexOf in interface IntList
      Overrides:
      indexOf in class AbstractIntList
      Parameters:
      c - the element to find.
      Returns:
      the index of the first occurance of the specified element in this list; returns -1, if the element is not contained in this list.
    • indexOf

      public int indexOf(int index, int c)
      Description copied from interface: IntList
      Returns the index of the first occurance of a specified element in this list after or at a specified index.
      Specified by:
      indexOf in interface IntList
      Overrides:
      indexOf in class AbstractIntList
      Parameters:
      index - the index at which to start the search.
      c - the element to find.
      Returns:
      the index of the first occurance of the specified element in this list; returns -1, if the element is not contained in this list.
      Since:
      1.2
    • lastIndexOf

      public int lastIndexOf(int c)
      Description copied from interface: IntList
      Returns the index of the last occurance of a specified element in this list.
      Specified by:
      lastIndexOf in interface IntList
      Overrides:
      lastIndexOf in class AbstractIntList
      Parameters:
      c - the element to find.
      Returns:
      the index of the last occurance of the specified element in this list; returns -1, if the element is not contained in this list.
    • remove

      public boolean remove(int v)
      Description copied from interface: IntCollection
      Removes a specified element from this collection.
      Specified by:
      remove in interface IntCollection
      Overrides:
      remove in class AbstractIntCollection
      Parameters:
      v - the int value to remove from this collection.
      Returns:
      true if this collection was modified as a result of removing v; returns false otherwise.
    • toArray

      public int[] toArray()
      Description copied from interface: IntCollection
      Returns the elements of this collection as an array.
      Specified by:
      toArray in interface IntCollection
      Overrides:
      toArray in class AbstractIntCollection
      Returns:
      a new array containing the elements of this collection.
    • toArray

      public int[] toArray(int[] a)
      Description copied from interface: IntCollection
      Returns the elements of this collection as an array.
      Specified by:
      toArray in interface IntCollection
      Overrides:
      toArray in class AbstractIntCollection
      Parameters:
      a - an array to fill with the elements of this collection; if a is null or not big enough to contain all the elements of this collection, an new array is allocated, and a is not changed.
      Returns:
      a, if a has room for all the elements of this collection; otherwise a new array is allocated, filled with the elements of this collection, and returned.
    • equals

      public boolean equals(Object obj)
      Description copied from interface: IntCollection
      Indicates whether this collection is equal to some object.
      Specified by:
      equals in interface IntCollection
      Overrides:
      equals in class AbstractIntList
      Parameters:
      obj - the object with which to compare this collection.
      Returns:
      true if this collection is equals to obj; returns false otherwise.
    • hashCode

      public int hashCode()
      Description copied from interface: IntCollection
      Returns a hash code value for this collection.
      Specified by:
      hashCode in interface IntCollection
      Overrides:
      hashCode in class AbstractIntList
      Returns:
      a hash code value for this collection.