Class IntArray


  • public class IntArray
    extends java.lang.Object
    A resizable, ordered or unordered int array. Avoids the boxing that occurs with ArrayList. 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 Description
      int[] items  
      boolean ordered  
      int size  
    • Constructor Summary

      Constructors 
      Constructor Description
      IntArray()
      Creates an ordered array with a capacity of 16.
      IntArray​(boolean ordered, int capacity)  
      IntArray​(boolean ordered, int[] array)
      Creates a new array containing the elements in the specified array.
      IntArray​(int capacity)
      Creates an ordered array with the specified capacity.
      IntArray​(int[] array)
      Creates a new ordered array containing the elements in the specified array.
      IntArray​(IntArray array)
      Creates a new array containing the elements in the specific array.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int value)  
      void addAll​(int[] array)  
      void addAll​(int[] array, int offset, int length)  
      void addAll​(IntArray array)  
      void addAll​(IntArray array, int offset, int length)  
      void clear()  
      boolean contains​(int value)  
      int[] ensureCapacity​(int additionalCapacity)
      Increases the size of the backing array to acommodate the specified number of additional items.
      int get​(int index)  
      int indexOf​(int value)  
      void insert​(int index, int value)  
      int peek()
      Returns the last item.
      int pop()
      Removes and returns the last item.
      int removeIndex​(int index)
      Removes and returns the item at the specified index.
      boolean removeValue​(int value)  
      protected int[] resize​(int newSize)  
      void reverse()  
      void set​(int index, int value)  
      void shrink()
      Reduces the size of the backing array to the size of the actual items.
      void sort()  
      void swap​(int first, int second)  
      int[] toArray()  
      java.lang.String toString()  
      java.lang.String toString​(java.lang.String separator)  
      void truncate​(int newSize)
      Reduces the size of the array to the specified size.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • items

        public int[] items
      • size

        public int size
      • ordered

        public boolean ordered
    • Constructor Detail

      • IntArray

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

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

        public IntArray​(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.
      • IntArray

        public IntArray​(IntArray 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.
      • IntArray

        public IntArray​(int[] 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.
      • IntArray

        public IntArray​(boolean ordered,
                        int[] array)
        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)
      • addAll

        public void addAll​(IntArray array)
      • addAll

        public void addAll​(IntArray array,
                           int offset,
                           int length)
      • addAll

        public void addAll​(int[] array)
      • addAll

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

        public int get​(int index)
      • set

        public void set​(int index,
                        int value)
      • insert

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

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

        public boolean contains​(int value)
      • indexOf

        public int indexOf​(int value)
      • removeValue

        public boolean removeValue​(int value)
      • removeIndex

        public int removeIndex​(int index)
        Removes and returns the item at the specified index.
      • pop

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

        public int peek()
        Returns the last item.
      • clear

        public void clear()
      • shrink

        public void 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.
      • ensureCapacity

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

        protected int[] 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 int[] toArray()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(java.lang.String separator)