E - object type this bag holdspublic class IntBag extends Object
| Modifier and Type | Field and Description |
|---|---|
protected int |
size
The amount of elements contained in 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 e)
Adds the specified element to the end of this bag.
|
void |
clear()
Removes all of the elements from this bag.
|
boolean |
contains(int e)
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.
|
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.
|
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.
|
int |
safeGet(int index)
Returns the element at the specified position in Bag.
|
void |
set(int index,
int e)
Set element at specified index in the bag.
|
int |
size()
Returns the number of elements in this bag.
|
String |
toString() |
public IntBag()
public IntBag(int capacity)
capacity - the initial capacity of Bagpublic 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 removedArrayIndexOutOfBoundsExceptionpublic boolean contains(int e)
e - element to checktrue if the bag contains this elementpublic int get(int index)
throws ArrayIndexOutOfBoundsException
index - index of the element to returnArrayIndexOutOfBoundsExceptionpublic int safeGet(int index)
index - index of the element to returnpublic int size()
public int getCapacity()
public boolean isIndexWithinBounds(int index)
index - index to checktrue if the index is within boundspublic boolean isEmpty()
true if this bag contains no elementspublic void add(int e)
If required, it also increases the capacity of the bag.
e - element to be added to this listpublic void set(int index,
int e)
index - position of elemente - 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()Copyright © 2014. All Rights Reserved.