Class SimpleArray<T>

  • Type Parameters:
    T - type of elements in the array
    All Implemented Interfaces:
    java.lang.Iterable<T>

    public class SimpleArray<T>
    extends java.lang.Object
    implements java.lang.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.
    If you don't have any of those needs, then you might as well use a standard ArrayList instead.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private T[] elements  
      private int size  
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleArray​(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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(T element)
      Add an element to the end of the list.
      void addAll​(T[] src)
      Add all elements from an array.
      private void addAll​(T[] src, int offset, int len)
      Add a portion of elements from an array.
      protected int calculateNewCapacity​(int currentCapacity)  
      void clear()
      Clear all elements.
      void copyTo​(T[] dst, int offset)
      Copy all elements from the backing array to another array.
      T[] elements()
      Get an array with all elements.
      T get​(int index)
      Get the element at the specified index.
      java.util.Iterator<T> 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
    • Field Detail

      • elements

        private T[] elements
      • size

        private int size
    • Constructor Detail

      • SimpleArray

        public SimpleArray​(T[] initial)
        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

        public SimpleArray​(T[] initial,
                           int size)
        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 storage
        size - number of preallocated elements
    • Method Detail

      • get

        public T get​(int index)
        Get the element at the specified index.
        Parameters:
        index - element index
        Returns:
        element at index
      • add

        public void add​(T element)
        Add an element to the end of the list.
        Parameters:
        element - element to add
      • addAll

        public void addAll​(T[] src)
        Add all elements from an array.
        Parameters:
        src - array containing elements to add
      • addAll

        private void addAll​(T[] src,
                            int offset,
                            int len)
        Add a portion of elements from an array.
        Parameters:
        src - array containing elements to add
        offset - offset in array to start adding elements from
        len - number of elements from the array to add
      • copyTo

        public void copyTo​(T[] dst,
                           int offset)
        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 to
        offset - 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

        public T[] 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

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>