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.

@NotThreadSafe public class DynamicArray<T> extends Object
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

    Constructors
    Constructor
    Description
    DynamicArray(int initialCapacity, IntFunction<T[]> allocator)
    Constructs a new GenericList with the specified initial capacity and array allocator.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T value)
    Adds a new element to the dynamic array.
    T[]
    Retrieves the underlying array that backs this dynamic array.
    void
    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.
    int
    Returns the length of the internal array backing this dynamic array.
    void
    resize(int target)
    Resizes the internal array to accommodate the specified target size.
    int
    Returns the number of elements currently stored in the dynamic array.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DynamicArray

      public DynamicArray(int initialCapacity, @Nonnull IntFunction<T[]> allocator)
      Constructs a new GenericList with 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 type T[] with the specified capacity. This allocator must not be null.
  • 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

      @Nonnull public T[] 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 to null, effectively clearing the contents of the array. The underlying array itself is not resized or released.
    • get

      @Nullable public T get(int index)
      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 an ArrayIndexOutOfBoundsException.
      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 null if no element exists at the index or the element is explicitly set to null.
    • add

      public void add(@Nonnull T value)
      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.