Class IntList

java.lang.Object
com.github.tommyettinger.gand.ds.IntList
All Implemented Interfaces:
IntCollection

public class IntList extends Object implements IntCollection
A resizable, insertion-ordered int list. Primitive-backed, so it avoids the boxing that occurs with an ArrayList of Integer. This tries to imitate most of the List interface, though it can't implement it without boxing its items. Has a primitive iterator accessible via iterator().
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A IntIterator, plus ListIterator methods, over the elements of a IntList.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int[]
     
     
     
    protected int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an ordered list with a capacity of 10.
    IntList(int capacity)
    Creates an ordered list with the specified capacity.
    IntList(int[] array)
    Creates a new list containing the elements in the specified array.
    IntList(int[] array, int startIndex, int count)
    Creates a new list containing the elements in the specified array.
    Creates a new list containing the items in the specified IntCollection.
    Creates a new list containing the elements in the given list.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(int value)
     
    void
    add(int value1, int value2)
     
    void
    add(int value1, int value2, int value3)
     
    void
    add(int value1, int value2, int value3, int value4)
     
    boolean
    addAll(int... array)
     
    boolean
    addAll(int[] array, int offset, int length)
     
    boolean
     
    boolean
    addAll(IntList list, int offset, int count)
     
    void
    Effectively removes all items from this IntList.
    boolean
    contains(int value)
     
    boolean
    Returns true if this IntList contains, at least once, every item in other; otherwise returns false.
    div(int value)
    Divides each item in this IntList by value, stores it in this and returns it.
    void
    div(int index, int value)
     
    boolean
    duplicateRange(int index, int count)
    Inserts the specified number of items at the specified index.
    int[]
    ensureCapacity(int additionalCapacity)
    Increases the size of the backing array to accommodate the specified number of additional items.
    boolean
    equals(Object object)
     
    int
    Returns the first item.
    int
    get(int index)
     
    int
     
    int
    indexOf(int value)
    Returns the first index in this list that contains the specified value, or -1 if it is not present.
    void
    insert(int index, int value)
     
    boolean
    Returns true if the list is empty.
    Returns a Java 8 primitive iterator over the int items in this IntList.
    int
    lastIndexOf(int value)
    Returns the last index in this list that contains the specified value, or -1 if it is not present.
    minus(int value)
    Takes each item in this IntList and subtracts value, stores it in this and returns it.
    void
    minus(int index, int value)
     
    boolean
    Returns true if the list has one or more items, or false otherwise.
    int
    Returns the last item.
    plus(int value)
    Adds value to each item in this IntList, stores it in this and returns it.
    void
    plus(int index, int value)
     
    int
    pop()
    Removes and returns the last item.
    int
    random(Random random)
    Returns a random item from the list, or zero if the list is empty.
    rem(int value)
    Gets the remainder of each item in this IntList with value, stores it in this and returns it.
    void
    rem(int index, int value)
     
    boolean
    remove(int value)
    Removes the first occurrence of value from this IntList, returning true if anything was removed.
    boolean
    Removes from this IntList all occurrences of any elements contained in the specified collection.
    int
    removeAt(int index)
    Removes and returns the item at the specified index.
    boolean
    Removes from this IntList element-wise occurrences of elements contained in the specified collection.
    void
    removeRange(int start, int end)
    Removes the items between the specified start index, inclusive, and end index, exclusive.
    protected int[]
    resize(int newSize)
     
    boolean
    Removes all items from this IntList that are not present somewhere in other, any number of times.
    void
     
    void
    set(int index, int value)
     
    int[]
    setSize(int newSize)
    Sets the list size, leaving any values beyond the current size undefined.
    int[]
    Reduces the size of the backing array to the size of the actual items.
    void
    shuffle(Random random)
     
    int
     
    void
     
    void
    sort(int from, int to, IntComparator c)
    Sorts the specified range of elements according to the order induced by the specified comparator using mergesort, or Arrays.sort(int[], int, int) if c is null.
    void
    Sorts all elements according to the order induced by the specified comparator using IntComparators.sort(int[], int, int, IntComparator).
    void
    swap(int first, int second)
     
    times(int value)
    Multiplies each item in this IntList by value, stores it in this and returns it.
    void
    times(int index, int value)
     
    int[]
    Allocates a new int array with size elements and fills it with the items in this.
    int[]
    toArray(int[] array)
    If array.length at least equal to size(), this copies the contents of this into array and returns it; otherwise, it allocates a new int array that can fit all the items in this, and proceeds to copy into that and return that.
     
    toString(String separator)
     
    void
    truncate(int newSize)
    Reduces the size of the list to the specified size.
    static IntList
    with(int item)
     
    static IntList
    with(int... array)
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • IntList

      public IntList()
      Creates an ordered list with a capacity of 10.
    • IntList

      public IntList(int capacity)
      Creates an ordered list with the specified capacity.
      Parameters:
      capacity - Any elements added beyond this will cause the backing array to be grown.
    • IntList

      public IntList(IntList list)
      Creates a new list containing the elements in the given list. The new list will be ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      Parameters:
      list - another IntList (or IntBag) to copy from
    • IntList

      public IntList(int[] array)
      Creates a new list containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      Parameters:
      array - an int array to copy from
    • IntList

      public IntList(int[] array, int startIndex, int count)
      Creates a new list containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      Parameters:
      array - a non-null int array to add to this list
      startIndex - the first index in array to use
      count - how many items to use from array
    • IntList

      public IntList(IntCollection coll)
      Creates a new list containing the items in the specified IntCollection.
      Parameters:
      coll - a primitive collection that will have its contents added to this
  • Method Details

    • size

      public int size()
      Specified by:
      size in interface IntCollection
    • add

      public boolean add(int value)
      Specified by:
      add in interface IntCollection
    • add

      public void add(int value1, int value2)
    • add

      public void add(int value1, int value2, int value3)
    • add

      public void add(int value1, int value2, int value3, int value4)
    • addAll

      public boolean addAll(IntList list)
    • addAll

      public boolean addAll(IntList list, int offset, int count)
    • addAll

      public boolean addAll(int... array)
      Specified by:
      addAll in interface IntCollection
    • addAll

      public boolean addAll(int[] array, int offset, int length)
      Specified by:
      addAll in interface IntCollection
    • get

      public int get(int index)
    • set

      public void set(int index, int value)
    • plus

      public void plus(int index, int value)
    • plus

      public IntList plus(int value)
      Adds value to each item in this IntList, stores it in this and returns it. The presence of this method allows Kotlin code to use the + operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).
      Parameters:
      value - each item in this will be assigned item + value
      Returns:
      this for chaining and Kotlin compatibility
    • times

      public void times(int index, int value)
    • times

      public IntList times(int value)
      Multiplies each item in this IntList by value, stores it in this and returns it. The presence of this method allows Kotlin code to use the * operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).
      Parameters:
      value - each item in this will be assigned item * value
      Returns:
      this for chaining and Kotlin compatibility
    • minus

      public void minus(int index, int value)
    • minus

      public IntList minus(int value)
      Takes each item in this IntList and subtracts value, stores it in this and returns it. This is just a minor convenience in Java, but the presence of this method allows Kotlin code to use the - operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).
      Parameters:
      value - each item in this will be assigned item - value
      Returns:
      this for chaining and Kotlin compatibility
    • div

      public void div(int index, int value)
    • div

      public IntList div(int value)
      Divides each item in this IntList by value, stores it in this and returns it. The presence of this method allows Kotlin code to use the / operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).
      Parameters:
      value - each item in this will be assigned item / value
      Returns:
      this for chaining and Kotlin compatibility
    • rem

      public void rem(int index, int value)
    • rem

      public IntList rem(int value)
      Gets the remainder of each item in this IntList with value, stores it in this and returns it. The presence of this method allows Kotlin code to use the % operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).
      Parameters:
      value - each item in this will be assigned item % value
      Returns:
      this for chaining and Kotlin compatibility
    • insert

      public void insert(int index, int value)
    • duplicateRange

      public boolean duplicateRange(int index, int count)
      Inserts the specified number of items at the specified index. The new items will have values equal to the values at those indices before the insertion, and the previous values will be pushed to after the duplicated range.
      Parameters:
      index - the first index to duplicate
      count - how many items to duplicate
    • swap

      public void swap(int first, int second)
    • contains

      public boolean contains(int value)
      Specified by:
      contains in interface IntCollection
    • containsAll

      public boolean containsAll(IntList other)
      Returns true if this IntList contains, at least once, every item in other; otherwise returns false.
      Parameters:
      other - an IntList
      Returns:
      true if this contains every item in other, otherwise false
    • indexOf

      public int indexOf(int value)
      Returns the first index in this list that contains the specified value, or -1 if it is not present.
      Parameters:
      value - a int value to search for
      Returns:
      the first index of the given value, or -1 if it is not present
    • lastIndexOf

      public int lastIndexOf(int value)
      Returns the last index in this list that contains the specified value, or -1 if it is not present.
      Parameters:
      value - a int value to search for
      Returns:
      the last index of the given value, or -1 if it is not present
    • remove

      public boolean remove(int value)
      Removes the first occurrence of value from this IntList, returning true if anything was removed. Otherwise, this returns false.
      Specified by:
      remove in interface IntCollection
      Parameters:
      value - the value to (attempt to) remove
      Returns:
      true if a value was removed, false if the IntList is unchanged
    • removeAt

      public int removeAt(int index)
      Removes and returns the item at the specified index. Note that this is equivalent to List.remove(int), but can't have that name because we also have remove(int) that removes a value, rather than an index.
      Parameters:
      index - the index of the item to remove and return
      Returns:
      the removed item
    • removeRange

      public void removeRange(int start, int end)
      Removes the items between the specified start index, inclusive, and end index, exclusive. Note that this takes different arguments than some other range-related methods; this needs a start index and an end index, rather than a count of items. This matches the behavior in the JDK collections.
      Parameters:
      start - the first index to remove, inclusive
      end - the last index (after what should be removed), exclusive
    • removeAll

      public boolean removeAll(IntCollection c)
      Removes from this IntList all occurrences of any elements contained in the specified collection.
      Specified by:
      removeAll in interface IntCollection
      Parameters:
      c - a primitive collection of int items to remove fully, such as another IntList or a IntDeque
      Returns:
      true if this list was modified.
    • removeEach

      public boolean removeEach(IntCollection c)
      Removes from this IntList element-wise occurrences of elements contained in the specified collection. Note that if a value is present more than once in this IntList, only one of those occurrences will be removed for each occurrence of that value in c. If c has the same contents as this IntList or has additional items, then removing each of c will clear this.
      Specified by:
      removeEach in interface IntCollection
      Parameters:
      c - a primitive collection of int items to remove one-by-one, such as another IntList or a IntDeque
      Returns:
      true if this list was modified.
    • retainAll

      public boolean retainAll(IntCollection other)
      Removes all items from this IntList that are not present somewhere in other, any number of times.
      Specified by:
      retainAll in interface IntCollection
      Parameters:
      other - a IntCollection that contains the items that this should keep, whenever present
      Returns:
      true if this IntList changed as a result of this call, otherwise false
    • pop

      public int pop()
      Removes and returns the last item.
      Returns:
      the last item, removed from this
    • peek

      public int peek()
      Returns the last item.
      Returns:
      the last item, without modifying this
    • first

      public int first()
      Returns the first item.
      Specified by:
      first in interface IntCollection
      Returns:
      the first item, without modifying this
    • notEmpty

      public boolean notEmpty()
      Returns true if the list has one or more items, or false otherwise.
      Specified by:
      notEmpty in interface IntCollection
      Returns:
      true if the list has one or more items, or false otherwise
    • isEmpty

      public boolean isEmpty()
      Returns true if the list is empty.
      Specified by:
      isEmpty in interface IntCollection
      Returns:
      true if the list is empty, or false if it has any items
    • clear

      public void clear()
      Effectively removes all items from this IntList. This is done simply by setting size to 0; because a int item isn't a reference, it doesn't need to be set to null.
      Specified by:
      clear in interface IntCollection
    • shrink

      public int[] shrink()
      Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.
      Returns:
      items; this will be a different reference if this resized
    • ensureCapacity

      public int[] ensureCapacity(int additionalCapacity)
      Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.
      Returns:
      items; this will be a different reference if this resized
    • setSize

      public int[] setSize(int newSize)
      Sets the list size, leaving any values beyond the current size undefined.
      Returns:
      items; this will be a different reference if this resized to a larger capacity
    • resize

      protected int[] resize(int newSize)
    • sort

      public void sort()
    • sort

      public void sort(IntComparator c)
      Sorts all elements according to the order induced by the specified comparator using IntComparators.sort(int[], int, int, IntComparator). If c is null, this instead delegates to sort(), which uses Arrays.sort(int[]), and does not always run in-place.

      This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. The sorting algorithm is an in-place mergesort that is significantly slower than a standard mergesort, as its running time is O(n (log n)2), but it does not allocate additional memory; as a result, it can be used as a generic sorting algorithm.

      Parameters:
      c - the comparator to determine the order of the IntList
    • sort

      public void sort(int from, int to, IntComparator c)
      Sorts the specified range of elements according to the order induced by the specified comparator using mergesort, or Arrays.sort(int[], int, int) if c is null. This purely uses IntComparators.sort(int[], int, int, IntComparator), and you can see its docs for more information.
      Parameters:
      from - the index of the first element (inclusive) to be sorted.
      to - the index of the last element (exclusive) to be sorted.
      c - the comparator to determine the order of the IntList
    • reverse

      public void reverse()
    • shuffle

      public void shuffle(Random random)
    • truncate

      public void truncate(int newSize)
      Reduces the size of the list to the specified size. If the list is already smaller than the specified size, no action is taken.
    • random

      public int random(Random random)
      Returns a random item from the list, or zero if the list is empty.
      Parameters:
      random - a Random or a subclass, such as any from juniper
      Returns:
      a randomly selected item from this, or 0 if this is empty
    • toArray

      public int[] toArray()
      Allocates a new int array with size elements and fills it with the items in this.
      Specified by:
      toArray in interface IntCollection
      Returns:
      a new int array with the same contents as this
    • toArray

      public int[] toArray(int[] array)
      If array.length at least equal to size(), this copies the contents of this into array and returns it; otherwise, it allocates a new int array that can fit all the items in this, and proceeds to copy into that and return that.
      Specified by:
      toArray in interface IntCollection
      Parameters:
      array - a int array that will be modified if it can fit size() items
      Returns:
      array, if it had sufficient size, or a new array otherwise, either with a copy of this
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface IntCollection
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object object)
      Specified by:
      equals in interface IntCollection
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String separator)
    • iterator

      public IntList.IntListIterator iterator()
      Returns a Java 8 primitive iterator over the int items in this IntList. Iterates in order.
      This will reuse one of two iterators in this IntList; this does not allow nested iteration. Use IntListIterator(IntList) to nest iterators.
      Specified by:
      iterator in interface IntCollection
      Returns:
      a IntIterator; use its nextInt() method instead of next()
    • with

      public static IntList with(int item)
    • with

      public static IntList with(int... array)
      See Also: