Package ints
Class IntSet
- java.lang.Object
-
- ints.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 newIntSet
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)
Returnstrue
if the set contains the specified element, and returnsfalse
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()
Returnsjava.util.Arrays.toString(this.toArray())
.
-
-
-
Method Detail
-
contains
public boolean contains(int element)
Returnstrue
if the set contains the specified element, and returnsfalse
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, andfalse
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, andfalse
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
- ifindex < 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
- ifcapacity < 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 eachj
satisfying0 < j && j < this.size()
- Returns:
- an array containing the elements in this set
-
toString
public java.lang.String toString()
Returnsjava.util.Arrays.toString(this.toArray())
.- Overrides:
toString
in classjava.lang.Object
- Returns:
java.util.Arrays.toString(this.toArray())
-
-