Contents
Class DynamicArray<T>
java.lang.Object
com.hypixel.hytale.builtin.adventure.worldevents.util.DynamicArray<T>
- Type Parameters:
T- the type of elements stored in this list.
A dynamic array of a generic type. This is used in place of ArrayLists where the
backing array must be of type
T[] and not Object[].-
Constructor Summary
ConstructorsConstructorDescriptionDynamicArray(int initialCapacity, IntFunction<T[]> allocator) Constructs a newGenericListwith the specified initial capacity and array allocator. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new element to the dynamic array.T[]array()Retrieves the underlying array that backs this dynamic array.voidclear()Removes all elements from the dynamic array and resets its size to zero.get(int index) Retrieves the element at the specified index from the dynamic array.intlength()Returns the length of the internal array backing this dynamic array.voidresize(int target) Resizes the internal array to accommodate the specified target size.intsize()Returns the number of elements currently stored in the dynamic array.
-
Constructor Details
-
DynamicArray
Constructs a newGenericListwith the specified initial capacity and array allocator. The allocator function is used to create the internal array that backs this dynamic list.- Parameters:
initialCapacity- the initial capacity of the internal array. This value determines the number of elements the list can initially hold before needing to resize.allocator- a function that generates a new array of typeT[]with the specified capacity. This allocator must not benull.
-
-
Method Details
-
size
public int size()Returns the number of elements currently stored in the dynamic array.- Returns:
- the number of elements in the array.
-
length
public int length()Returns the length of the internal array backing this dynamic array. This provides the total capacity of the array, regardless of how many elements are currently stored within it.- Returns:
- the length of the internal array.
-
array
Retrieves the underlying array that backs this dynamic array. Modifications to the returned array will directly impact the state of this dynamic array.- Returns:
- the internal array of type
T[]that backs this dynamic array.
-
clear
public void clear()Removes all elements from the dynamic array and resets its size to zero. This operation sets all positions in the backing array tonull, effectively clearing the contents of the array. The underlying array itself is not resized or released. -
get
Retrieves the element at the specified index from the dynamic array. If the specified index is out of bounds, the behavior of this method is undefined and may result in anArrayIndexOutOfBoundsException.- Parameters:
index- the position of the element to retrieve. Must be within the bounds of the array (0 invalid input: '<'= index invalid input: '<' size).- Returns:
- the element at the specified index, or
nullif no element exists at the index or the element is explicitly set tonull.
-
add
Adds a new element to the dynamic array. If the current capacity of the array is insufficient to accommodate the new element, the internal array is resized before the element is added.- Parameters:
value- the element to be added to the dynamic array. Must not be null.
-
resize
public void resize(int target) Resizes the internal array to accommodate the specified target size. If the target size exceeds the current capacity, a new array with an increased capacity is allocated and the contents of the current array are copied into the new array. If the target size is smaller than the current capacity, the array may be downsized, truncating elements beyond the target size.- Parameters:
target- the desired size for the internal array. This value is used to determine the capacity of the resized array.
-