Class ArrayIntSet

java.lang.Object
it.unimi.di.prog2.h17.AbstractIntSet
it.unimi.di.prog2.h17.ArrayIntSet
All Implemented Interfaces:
Iterable<Integer>

public class ArrayIntSet extends AbstractIntSet
ArrayIntSets are mutable, unbounded sets of integers.

A typical ArrayIntSet is \( S = \{x_1, \ldots, x_n \} \).

  • Field Details

    • INITIAL_CAPACITY

      private static final int INITIAL_CAPACITY
      The initial capacity of the internal array.
      See Also:
    • els

      private int[] els
      The List containing this set elements.
  • Constructor Details

    • ArrayIntSet

      public ArrayIntSet()
      Initializes this set to be empty.

      Builds the set \( S = \varnothing \).

  • Method Details

    • indexOf

      private int indexOf(int x)
      Looks for a given element in this set.
      Parameters:
      x - the element to look for.
      Returns:
      an index i such that els[i] == x if the element belongs to this set, or -1
    • insert

      public void insert(int x)
      Adds the given element to this set.

      This method modifies the object, that is: \( S' = S \cup \{ x \} \).

      Specified by:
      insert in class AbstractIntSet
      Parameters:
      x - the element to be added.
    • remove

      public void remove(int x)
      Removes the given element from this set.

      This method modifies the object, that is: \( S' = S \setminus \{ x \} \).

      Specified by:
      remove in class AbstractIntSet
      Parameters:
      x - the element to be removed.
    • choose

      public int choose() throws NoSuchElementException
      Returns an element from this set.
      Overrides:
      choose in class AbstractIntSet
      Returns:
      an arbitrary element from this set.
      Throws:
      NoSuchElementException - if this set is empty.
    • iterator

      public Iterator<Integer> iterator()