Package it.unimi.di.prog2.h22
Class UnboundedSet<T>
java.lang.Object
it.unimi.di.prog2.h22.UnboundedSet<T>
- All Implemented Interfaces:
Iterable<T>
UnboundedSet
s are mutable, unbounded sets of elements of a given type.
A typical UnboundedSet is \( S = \{x_1, \ldots, x_n \} \).
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionchoose()
Returns an element from this set.private int
Looks for a given element in this set.void
Adds the given element to this set.boolean
Tells if the given element is in this set.iterator()
void
Removes the given element from this set.private boolean
repOk()
int
size()
Returns the cardinality of this set.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
els
TheList
containing this set elements.
-
-
Constructor Details
-
UnboundedSet
public UnboundedSet()Initializes this set to be empty.Builds the set \( S = \varnothing \).
-
-
Method Details
-
repOk
private boolean repOk() -
getIndex
Looks for a given element in this set.- Parameters:
x
- the element to look for.- Returns:
- the index where
x
appears inels
if the element belongs to this set, or -1
-
insert
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
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
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:
EmptyException
- if this set is empty.
-
toString
-
iterator
-