Class ArrayIntSet
java.lang.Object
it.unimi.di.prog2.h13.ArrayIntSet
- Direct Known Subclasses:
MaxIntSet
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int[]TheListcontaining this set elements.private static final intThe initial capacity of the internal array.private intThe number of elements in this set. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintchoose()Returns an element from this set.booleaninthashCode()private intindexOf(int x) Looks for a given element in this set.voidinsert(int x) Adds the given element to this set.booleanisIn(int x) Tells if the given element is in this set.iterator()voidremove(int x) Removes the given element from this set.intsize()Returns the cardinality of this set.toString()Methods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
INITIAL_CAPACITY
private static final int INITIAL_CAPACITYThe initial capacity of the internal array.- See Also:
-
els
private int[] elsTheListcontaining this set elements. -
size
private int sizeThe number of elements in this set.
-
-
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
isuch thatels[i] == xif 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 \} \).
- 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 \} \).
- Parameters:
x- the element to be removed.
-
isIn
public boolean isIn(int x) Tells if the given element is in this set.Answers the question \( x\in S \).
- Parameters:
x- the element to look for.- Returns:
- whether the given element belongs to this set, or not.
-
size
public int size()Returns the cardinality of this set.Responds with \( |S| \).
- Returns:
- the size of this set.
-
choose
Returns an element from this set.- Returns:
- an arbitrary element from this set.
- Throws:
NoSuchElementException- if this set is empty.
-
toString
-
equals
-
hashCode
-
iterator
-