public class IntBag extends Object implements ImmutableIntBag
Modifier and Type | Field and Description |
---|---|
protected int |
size
The number of values stored by this bag.
|
Constructor and Description |
---|
IntBag()
Constructs an empty Bag with an initial capacity of 64.
|
IntBag(int capacity)
Constructs an empty Bag with the specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int value)
Adds the specified element to the end of this bag.
|
void |
addAll(IntBag other)
Adds the specified elements to the end of this bag.
|
void |
clear()
Removes all of the elements from this bag.
|
boolean |
contains(int value)
Check if bag contains this element.
|
void |
ensureCapacity(int index)
Check if an item, if added at the given item will fit into the bag.
|
boolean |
equals(Object o) |
int |
get(int index)
Returns the element at the specified position in Bag.
|
int |
getCapacity()
Returns the number of elements the bag can hold without growing.
|
int[] |
getData()
Returns this bag's underlying array.
|
int |
hashCode() |
int |
indexOf(int value)
Find index of element.
|
boolean |
isEmpty()
Returns true if this bag contains no elements.
|
boolean |
isIndexWithinBounds(int index)
Checks if the internal storage supports this index.
|
int |
remove(int index)
Removes the element at the specified position in this Bag.
|
boolean |
removeValue(int value)
Removes the first occurrence of the value from this IntBag, if
it is present.
|
void |
set(int index,
int value)
Set element at specified index in the bag.
|
void |
setSize(int size)
Set the size.
|
int |
size()
Returns the number of elements in this bag.
|
String |
toString() |
public IntBag()
public IntBag(int capacity)
capacity
- the initial capacity of Bagpublic boolean removeValue(int value) throws ArrayIndexOutOfBoundsException
value
- the value to be removedArrayIndexOutOfBoundsException
public int remove(int index) throws ArrayIndexOutOfBoundsException
It does this by overwriting it was last element then removing last element
index
- the index of element to be removedArrayIndexOutOfBoundsException
public int indexOf(int value)
value
- element to check-1
if there is no such index.public boolean contains(int value)
contains
in interface ImmutableIntBag
value
- element to checktrue
if the bag contains this elementpublic int get(int index) throws ArrayIndexOutOfBoundsException
get
in interface ImmutableIntBag
index
- index of the element to returnArrayIndexOutOfBoundsException
public int size()
size
in interface ImmutableIntBag
public int getCapacity()
public boolean isIndexWithinBounds(int index)
index
- index to checktrue
if the index is within boundspublic boolean isEmpty()
isEmpty
in interface ImmutableIntBag
true
if this bag contains no elementspublic void add(int value)
If required, it also increases the capacity of the bag.
value
- element to be added to this listpublic void addAll(IntBag other)
If required, it also increases the capacity of the bag.
other
- elements to be added to this listpublic void set(int index, int value)
index
- position of elementvalue
- the elementpublic void ensureCapacity(int index)
If not, the bag capacity will be increased to hold an item at the index.
index
- index to checkpublic void clear()
The bag will be empty after this call returns.
public int[] getData()
Use with care.
size()
public void setSize(int size)
This will not resize the bag, nor will it clean up contents beyond the given size. Use with caution.
size
- the size to setCopyright © 2019. All rights reserved.