Package ints
Class IntSet
- java.lang.Object
-
- ints.IntSet
-
public class IntSet extends java.lang.ObjectClass
IntSetrepresents an indexed set of integers.Class
IntSetis not thread-safe.
-
-
Constructor Summary
Constructors Constructor Description IntSet(int capacity)Creates a newIntSetinstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(int element)Adds the specified element to this set.intcapacity()Returns the capacity of this set.voidclear()Removes all elements from this set.booleancontains(int element)Returnstrueif the set contains the specified element, and returnsfalseotherwise.intelementWithIndex(int index)Returns the specified element.booleanremove(int element)Removes the specified element from this set.voidsetCapacity(int capacity)Sets the capacity of this list to the specified value.intsize()Returns the number of elements in this set.int[]toArray()Returns an array containing the elements in this set.java.lang.StringtoString()Returnsjava.util.Arrays.toString(this.toArray()).
-
-
-
Method Detail
-
contains
public boolean contains(int element)
Returnstrueif the set contains the specified element, and returnsfalseotherwise.- Parameters:
element- an nonnegative integer- Returns:
trueif 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:
trueif the set was changed by the operation, andfalseotherwise
-
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:
trueif the set was changed by the operation, andfalseotherwise
-
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 eachjsatisfying0 < 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:
toStringin classjava.lang.Object- Returns:
java.util.Arrays.toString(this.toArray())
-
-