Package ints

Class IntSet


  • public class IntSet
    extends java.lang.Object

    Class IntSet represents an indexed set of integers.

    Class IntSet is not thread-safe.

    • Constructor Summary

      Constructors 
      Constructor Description
      IntSet​(int capacity)
      Creates a new IntSet instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(int element)
      Adds the specified element to this set.
      int capacity()
      Returns the capacity of this set.
      void clear()
      Removes all elements from this set.
      boolean contains​(int element)
      Returns true if the set contains the specified element, and returns false otherwise.
      int elementWithIndex​(int index)
      Returns the specified element.
      boolean remove​(int element)
      Removes the specified element from this set.
      void setCapacity​(int capacity)
      Sets the capacity of this list to the specified value.
      int size()
      Returns the number of elements in this set.
      int[] toArray()
      Returns an array containing the elements in this set.
      java.lang.String toString()
      Returns java.util.Arrays.toString(this.toArray()).
      • Methods inherited from class java.lang.Object

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

      • IntSet

        public IntSet​(int capacity)
        Creates a new IntSet instance.
        Parameters:
        capacity - the initial capacity of the set
        Throws:
        java.lang.IllegalArgumentException - if capacity < 0 || (capacity > (1 << 30))
    • Method Detail

      • contains

        public boolean contains​(int element)
        Returns true if the set contains the specified element, and returns false otherwise.
        Parameters:
        element - an nonnegative integer
        Returns:
        true if the set contains the specified element
      • add

        public boolean add​(int element)
        Adds the specified element to this set. The indexing of set elements immediately before and after this command is invoked may differ if the set is changed by the operation.
        Parameters:
        element - an integer to add to this set
        Returns:
        true if the set was changed by the operation, and false otherwise
      • remove

        public boolean remove​(int element)
        Removes the specified element from this set. The indexing of set elements immediately before and after this command is invoked may differ if the set is changed by the operation.
        Parameters:
        element - an integer to remove this set
        Returns:
        true if the set was changed by the operation, and false otherwise
      • elementWithIndex

        public int elementWithIndex​(int index)
        Returns the specified element.
        Parameters:
        index - an index of an element in this set
        Returns:
        the specified element
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= this.size()
      • clear

        public void clear()
        Removes all elements from this set.
      • size

        public int size()
        Returns the number of elements in this set.
        Returns:
        the number of elements in this set
      • capacity

        public int capacity()
        Returns the capacity of this set. The capacity of this set is the maximum number of elements that may be stored without allocating more memory.
        Returns:
        the capacity of this set
      • setCapacity

        public void setCapacity​(int capacity)
        Sets the capacity of this list to the specified value. The capacity of this set is the maximum number of elements that may be stored without allocating more memory.
        Parameters:
        capacity - the desired capacity
        Throws:
        java.lang.IllegalArgumentException - if capacity < this.size()
      • toArray

        public int[] toArray()
        Returns an array containing the elements in this set. The returned array will satisfy: this.toArray()[j]==this.elementWithIndex(j) for each j satisfying 0 < j && j < this.size()
        Returns:
        an array containing the elements in this set
      • 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())