Class ShortArray



  • public class ShortArray
    extends Object
    A resizable, ordered or unordered short array. Avoids the boxing that occurs with ArrayList<Short>. If unordered, this class avoids a memory copy when removing elements (the last element is moved to the removed element's position).
    • Field Summary

      Fields

      Modifier and Type Field and Description
      short[] items
       
      boolean ordered
       
      int size
       
    • Constructor Summary

      Constructors

      Constructor and Description
      ShortArray()
      Creates an ordered array with a capacity of 16.
      ShortArray(boolean ordered, int capacity)
       
      ShortArray(boolean ordered, short[] array, int startIndex, int count)
      Creates a new array containing the elements in the specified array.
      ShortArray(int capacity)
      Creates an ordered array with the specified capacity.
      ShortArray(short[] array)
      Creates a new ordered array containing the elements in the specified array.
      ShortArray(ShortArray array)
      Creates a new array containing the elements in the specific array.
    • Method Summary

      Modifier and Type Method and Description
      void add(int value)
      Casts the specified value to short and adds it.
      void add(short value)
       
      void addAll(short... array)
       
      void addAll(short[] array, int offset, int length)
       
      void addAll(ShortArray array)
       
      void addAll(ShortArray array, int offset, int length)
       
      void clear()
       
      boolean contains(short value)
       
      short[] ensureCapacity(int additionalCapacity)
      Increases the size of the backing array to accommodate the specified number of additional items.
      boolean equals(Object object)
       
      short first()
      Returns the first item.
      short get(int index)
       
      int hashCode()
       
      void incr(int index, short value)
       
      int indexOf(short value)
       
      void insert(int index, short value)
       
      int lastIndexOf(char value)
       
      void mul(int index, short value)
       
      short peek()
      Returns the last item.
      short pop()
      Removes and returns the last item.
      boolean removeAll(ShortArray array)
      Removes from this array all of elements contained in the specified array.
      short removeIndex(int index)
      Removes and returns the item at the specified index.
      void removeRange(int start, int end)
      Removes the items between the specified indices, inclusive.
      boolean removeValue(short value)
       
      protected short[] resize(int newSize)
       
      void reverse()
       
      void set(int index, short value)
       
      short[] setSize(int newSize)
      Sets the array size, leaving any values beyond the current size undefined.
      short[] shrink()
      Reduces the size of the backing array to the size of the actual items.
      void sort()
       
      void swap(int first, int second)
       
      short[] toArray()
       
      String toString()
       
      String toString(String separator)
       
      void truncate(int newSize)
      Reduces the size of the array to the specified size.
      static ShortArray with(short... array)
       
    • Field Detail

      • items

        public short[] items
      • size

        public int size
      • ordered

        public boolean ordered
    • Constructor Detail

      • ShortArray

        public ShortArray()
        Creates an ordered array with a capacity of 16.
      • ShortArray

        public ShortArray(int capacity)
        Creates an ordered array with the specified capacity.
      • ShortArray

        public ShortArray(boolean ordered,
                          int capacity)
        Parameters:
        ordered - If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
        capacity - Any elements added beyond this will cause the backing array to be grown.
      • ShortArray

        public ShortArray(ShortArray array)
        Creates a new array containing the elements in the specific array. The new array will be ordered if the specific array is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      • ShortArray

        public ShortArray(short[] array)
        Creates a new ordered array containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
      • ShortArray

        public ShortArray(boolean ordered,
                          short[] array,
                          int startIndex,
                          int count)
        Creates a new array containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.
        Parameters:
        ordered - If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
    • Method Detail

      • add

        public void add(int value)
        Casts the specified value to short and adds it.
      • add

        public void add(short value)
      • addAll

        public void addAll(ShortArray array,
                           int offset,
                           int length)
      • addAll

        public void addAll(short... array)
      • addAll

        public void addAll(short[] array,
                           int offset,
                           int length)
      • get

        public short get(int index)
      • set

        public void set(int index,
                        short value)
      • incr

        public void incr(int index,
                         short value)
      • mul

        public void mul(int index,
                        short value)
      • insert

        public void insert(int index,
                           short value)
      • swap

        public void swap(int first,
                         int second)
      • contains

        public boolean contains(short value)
      • indexOf

        public int indexOf(short value)
      • lastIndexOf

        public int lastIndexOf(char value)
      • removeValue

        public boolean removeValue(short value)
      • removeIndex

        public short removeIndex(int index)
        Removes and returns the item at the specified index.
      • removeRange

        public void removeRange(int start,
                                int end)
        Removes the items between the specified indices, inclusive.
      • removeAll

        public boolean removeAll(ShortArray array)
        Removes from this array all of elements contained in the specified array.
        Returns:
        true if this array was modified.
      • pop

        public short pop()
        Removes and returns the last item.
      • peek

        public short peek()
        Returns the last item.
      • first

        public short first()
        Returns the first item.
      • clear

        public void clear()
      • shrink

        public short[] shrink()
        Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.
        Returns:
        items
      • ensureCapacity

        public short[] ensureCapacity(int additionalCapacity)
        Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.
        Returns:
        items
      • setSize

        public short[] setSize(int newSize)
        Sets the array size, leaving any values beyond the current size undefined.
        Returns:
        items
      • resize

        protected short[] resize(int newSize)
      • sort

        public void sort()
      • reverse

        public void reverse()
      • truncate

        public void truncate(int newSize)
        Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken.
      • toArray

        public short[] toArray()
      • hashCode

        public int hashCode()
      • equals

        public boolean equals(Object object)
      • toString

        public String toString()