Class SimpleArray<T>
java.lang.Object
org.openjdk.jmc.common.collection.SimpleArray<T>
- Type Parameters:
T
- type of elements in the array
- All Implemented Interfaces:
Iterable<T>
An array that can be iterated over.
This class is useful in a few cases:
- You expect that there are a large, but unknown, number of elements that will be added. This class grows its internal storage array more aggressively to avoid multiple array copying. The back side to this is that it may waste more memory.
- You want to reuse a preallocated array.
-
Constructor Summary
ConstructorsConstructorDescriptionSimpleArray
(T[] initial) Create an instance backed by an array.SimpleArray
(T[] initial, int size) Create an instance backed by an array with a specified number of preallocated elements. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add an element to the end of the list.void
Add all elements from an array.protected int
calculateNewCapacity
(int currentCapacity) void
clear()
Clear all elements.void
Copy all elements from the backing array to another array.T[]
elements()
Get an array with all elements.get
(int index) Get the element at the specified index.iterator()
int
size()
Get the number of stored elements.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
SimpleArray
Create an instance backed by an array. The array content will be overwritten with elements that are subsequently added.- Parameters:
initial
- array to use for storage
-
SimpleArray
Create an instance backed by an array with a specified number of preallocated elements. The array content after the preallocated elements will be overwritten with elements that are subsequently added.- Parameters:
initial
- array to use for storagesize
- number of preallocated elements
-
-
Method Details
-
get
Get the element at the specified index.- Parameters:
index
- element index- Returns:
- element at index
-
add
Add an element to the end of the list.- Parameters:
element
- element to add
-
addAll
Add all elements from an array.- Parameters:
src
- array containing elements to add
-
copyTo
Copy all elements from the backing array to another array. The destination array must be large enough to fit all elements.- Parameters:
dst
- array to copy elements tooffset
- starting position in the destination array
-
calculateNewCapacity
protected int calculateNewCapacity(int currentCapacity) -
size
public int size()Get the number of stored elements. Note that this is not the same as the size of the backing array.- Returns:
- the number of stored elements
-
clear
public void clear()Clear all elements. Note that the backing array is not cleared. -
elements
Get an array with all elements. The array length will be equal to the number of elements.- Returns:
- an array with all elements
-
iterator
-