Package ints

Class IntList


  • public class IntList
    extends java.lang.Object

    Class IntList represents a list of integers. Class IntList supports a clear() method, but it does not support a remove() method.

    Class IntList is not thread-safe.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_INIT_CAPACITY
      The default initial capacity of an IntList, which is 16.
    • Constructor Summary

      Constructors 
      Constructor Description
      IntList()
      Constructs an IntList object with the default initial capacity.
      IntList​(int initCapacity)
      Constructs an IntList object with the specified initial capacity.
      IntList​(int[] ia)
      Constructs an IntList by cloning the specified array.
      IntList​(IntList intList)
      Constructs an IntList by copying the specified IntList.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int value)
      Adds the specified integer to the end of this list.
      int binarySearch​(int value)
      Returns an index associated with the specified value, or (-insertionPoint - 1) if the value is not found.
      void clear()
      Removes all elements from this list.
      int[] copyOf​(int newLength)
      Copies and returns this list of integers, truncating or padding with 0 as necessary so that the copy has the specified length.
      int[] copyOfRange​(int start, int end)
      Returns the specified range of elements adding 0 values to the end of the copied values if necessary so that the returned array has length end - start.
      int decrementAndGet​(int index)
      Decrements by one the element at the specified position in this list.
      int get​(int index)
      Returns the element at the specified position in this list.
      int getAndDecrement​(int index)
      Decrements by one the element at the specified position in this list.
      int getAndIncrement​(int index)
      Increments by one the element at the specified position in this list.
      int incrementAndGet​(int index)
      Increments by one the element at the specified position in this list.
      boolean isEmpty()
      Returns true if this list has no elements, and returns false otherwise.
      int pop()
      Removes and returns the last entry of this list.
      int set​(int index, int value)
      Replaces the element at the specified position in this list with the specified element.
      int size()
      Returns the number of elements in this list.
      void sort()
      Sorts the elements of this list in increasing order
      java.util.stream.IntStream stream()
      Returns an IntStream containing the sequence of elements in this list.
      int[] toArray()
      Returns an integer array containing the sequence of elements in this list.
      java.lang.String toString()
      Returns java.util.Arrays.toString(this.toArray())
      void truncate​(int newSize)
      Truncates this list of integer by removing all elements whose index is greater than or equal to the specified size.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEFAULT_INIT_CAPACITY

        public static final int DEFAULT_INIT_CAPACITY
        The default initial capacity of an IntList, which is 16.
        See Also:
        Constant Field Values
    • Constructor Detail

      • IntList

        public IntList()
        Constructs an IntList object with the default initial capacity.
        See Also:
        DEFAULT_INIT_CAPACITY
      • IntList

        public IntList​(int initCapacity)
        Constructs an IntList object with the specified initial capacity.
        Parameters:
        initCapacity - the initial capacity of this list
        Throws:
        java.lang.IllegalArgumentException - if initCapacity < 0
      • IntList

        public IntList​(int[] ia)
        Constructs an IntList by cloning the specified array.
        Parameters:
        ia - a list of integer values
        Throws:
        java.lang.NullPointerException - if ia == null
      • IntList

        public IntList​(IntList intList)
        Constructs an IntList by copying the specified IntList.
        Parameters:
        intList - a list of integer values
        Throws:
        java.lang.NullPointerException - if intList == null
    • Method Detail

      • add

        public void add​(int value)
        Adds the specified integer to the end of this list.
        Parameters:
        value - the integer to be added to the end of this list
      • pop

        public int pop()
        Removes and returns the last entry of this list.
        Returns:
        the last entry of this list
        Throws:
        java.lang.IndexOutOfBoundsException - if this.isEmpty() == true
      • get

        public int get​(int index)
        Returns the element at the specified position in this list.
        Parameters:
        index - the index of the element to be returned
        Returns:
        the element at the specified position in this list
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= this.size()
      • set

        public int set​(int index,
                       int value)
        Replaces the element at the specified position in this list with the specified element.
        Parameters:
        index - the index of the element to be replaced
        value - the value to be stored at the specified position in this list
        Returns:
        the previous value at the specified position in this list
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= this.size()
      • getAndIncrement

        public int getAndIncrement​(int index)
        Increments by one the element at the specified position in this list.
        Parameters:
        index - the index of the element to be incremented
        Returns:
        the previous element at the specified position in this list
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= this.size()
      • getAndDecrement

        public int getAndDecrement​(int index)
        Decrements by one the element at the specified position in this list.
        Parameters:
        index - the index of the element to be decremented
        Returns:
        the previous element at the specified position in this list
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= this.size()
      • incrementAndGet

        public int incrementAndGet​(int index)
        Increments by one the element at the specified position in this list.
        Parameters:
        index - the index of the element to be incremented
        Returns:
        the updated element at the specified position in this list
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= this.size()
      • decrementAndGet

        public int decrementAndGet​(int index)
        Decrements by one the element at the specified position in this list.
        Parameters:
        index - the index of the element to be decremented
        Returns:
        the updated element at the specified position in this list
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= this.size()
      • size

        public int size()
        Returns the number of elements in this list.
        Returns:
        the number of elements in this list
      • isEmpty

        public boolean isEmpty()
        Returns true if this list has no elements, and returns false otherwise.
        Returns:
        true if this list has no elements
      • sort

        public void sort()
        Sorts the elements of this list in increasing order
      • binarySearch

        public int binarySearch​(int value)
        Returns an index associated with the specified value, or (-insertionPoint - 1) if the value is not found. The returned value is undefined if the list of integers is not sorted in increasing order.
        Parameters:
        value - the value to be searched for
        Returns:
        an index associated with the specified value, or (-insertionPoint - 1) if the value is not found
      • copyOf

        public int[] copyOf​(int newLength)
        Copies and returns this list of integers, truncating or padding with 0 as necessary so that the copy has the specified length.
        Parameters:
        newLength - the length of the returned array
        Returns:
        a copy of this list of integers, truncating or padding with 0 as necessary so that the copy has the specified length
        Throws:
        java.lang.NegativeArraySizeException - if newLength < 0
      • copyOfRange

        public int[] copyOfRange​(int start,
                                 int end)
        Returns the specified range of elements adding 0 values to the end of the copied values if necessary so that the returned array has length end - start.
        Parameters:
        start - the start of the range to be copied (inclusive)
        end - the end of the range to be copied (exclusive)
        Returns:
        the specified range of elements
        Throws:
        java.lang.IndexOutOfBoundsException - if start < 0 || start > this.size()
        java.lang.NegativeArraySizeException - if end > start
      • truncate

        public void truncate​(int newSize)
        Truncates this list of integer by removing all elements whose index is greater than or equal to the specified size. The list is unchanged if the specified size is greater than the number of elements in the list.
        Parameters:
        newSize - the number of elements in the truncated list
        Throws:
        java.lang.IllegalArgumentException - if newSize < 0
      • toArray

        public int[] toArray()
        Returns an integer array containing the sequence of elements in this list.
        Returns:
        an integer array containing the sequence of elements in this list
      • stream

        public java.util.stream.IntStream stream()
        Returns an IntStream containing the sequence of elements in this list. The contract for this method is unspecified if this list is modified during use of the returned stream.
        Returns:
        an IntStream containing the sequence of elements in this list
      • clear

        public void clear()
        Removes all elements from this list.
      • toString

        public java.lang.String toString()
        Returns java.util.Arrays.toString(this.toArray())
        Overrides:
        toString in class java.lang.Object
        Returns:
        java.util.Arrays.toString(this.toArray())