Class IntVector


  • public class IntVector
    extends java.lang.Object
    An int vector is an array of int's. The vector grows dynamically as ints are added, but it never shrinks. The IntVector can also be used as an IntStack or a bit vector.

    Here is an example showing a vector being used:

     ...
     IntVector vec = new IntVector();
     vec.addElement(int1);
     vec.addElement(int22);
     ...
     vec.insertElementAt(int3, 3);
     vec.removeElementAt(2);
     if (vec.size() > 5)
     ...
     

    For efficiency, get and set is made directly through the public array. Please use the add and remove methods to manipulate the IntVector.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int count  
      int[] items
      This member is public for fast access.
    • Constructor Summary

      Constructors 
      Constructor Description
      IntVector()
      Constructs an empty vector.
      IntVector​(int size)
      Constructs an empty vector with a given initial size.
      IntVector​(int[] items)
      Constructs a vector by directly assigning the given int array.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addElement​(int obj)
      Adds an int to the end of the vector.
      void addElements​(int[] elements)
      Appends an array of integers at the end of this vector.
      boolean contains​(int v)
      Returns true if this vector contains the specified element.
      void copyInto​(int[] out)
      Copies the items of this vector into the given array, which must have at least the current size of this vector.
      void ensureBit​(int index)
      Useful method to use when this IntVector will act like a bit vector, through the methods isBitSet and setBit.
      int indexOf​(int elem)
      Finds the index of the given int.
      int indexOf​(int elem, int index)
      Finds the index of the given int.
      void insertElementAt​(int obj, int index)
      Inserts the object at the given index.
      boolean isBitSet​(int index)
      Used to let this int vector act like a bit vector.
      boolean isEmpty()
      Returns if this IntVector is empty.
      int peek()
      Returns the last int, without removing it.
      int pop()
      Returns the last int, removing it.
      void pop​(int howMany)
      Pops the given number of elements from this vector.
      void push​(int obj)
      Pushes an int.
      void qsort()
      Does a quick sort in the elements of this IntVector
      void removeAllElements()
      Sets all elements in this vector to 0 and its size to 0.
      void removeElement​(int obj)
      Deletes the given integer from this vector.
      void removeElementAt​(int index)
      Deletes the int reference at the given index.
      void reverse()
      Reverses the order of the elements in this vector.
      In a vector with n elements, the element of index 0 is moved to the index n-1, the element of index 1 is moved to the index n-2, and so on.
      void setBit​(int index, boolean on)
      Used to let this int vector act like a bit vector.
      void setSize​(int newSize)
      Sets the size of this vector.
      int size()
      Returns the number of ints in the vector.
      int[] toIntArray()
      Returns a new array with the added elements
      • Methods inherited from class java.lang.Object

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

      • items

        public int[] items
        This member is public for fast access. Always use the correct methods for add and remove, otherwise you'll be in trouble.
      • count

        protected int count
    • Constructor Detail

      • IntVector

        public IntVector()
        Constructs an empty vector.
      • IntVector

        public IntVector​(int size)
        Constructs an empty vector with a given initial size. The size is the initial size of the vector's internal int array. The vector will grow as needed when ints are added. SIZE CANNOT BE 0!
      • IntVector

        public IntVector​(int[] items)
        Constructs a vector by directly assigning the given int array. Changes on the original array will reflect the items of this array and vice-versa.
        Since:
        SuperWaba 5.11
    • Method Detail

      • ensureBit

        public void ensureBit​(int index)
        Useful method to use when this IntVector will act like a bit vector, through the methods isBitSet and setBit. Just call it with the maximum bit index what will be used (starting from 0); then you can safely use the two methods. This must be done because those methods does not check the bounds of the array.
      • isBitSet

        public boolean isBitSet​(int index)
        Used to let this int vector act like a bit vector.
        Returns:
        true if the bit specified is set. you must guarantee that the index exists in the vector.
      • setBit

        public void setBit​(int index,
                           boolean on)
        Used to let this int vector act like a bit vector. you must guarantee that the index exists in the vector.
      • push

        public void push​(int obj)
        Pushes an int.
      • pop

        public void pop​(int howMany)
        Pops the given number of elements from this vector.
        Since:
        SuperWaba 5.0
      • isEmpty

        public boolean isEmpty()
        Returns if this IntVector is empty.
        Since:
        TotalCross 1.0.
      • size

        public int size()
        Returns the number of ints in the vector.
      • indexOf

        public int indexOf​(int elem)
        Finds the index of the given int. The list is searched using a O(n) linear search through all the ints in the vector.
        Returns:
        -1 if the object is not found.
      • indexOf

        public int indexOf​(int elem,
                           int index)
        Finds the index of the given int. The list is searched using a O(n) linear search starting in startIndex up through all the ints in the vector.
        Returns:
        -1 if the object is not found.
      • removeElementAt

        public void removeElementAt​(int index)
        Deletes the int reference at the given index.
      • insertElementAt

        public void insertElementAt​(int obj,
                                    int index)
        Inserts the object at the given index. If index is less than 0 or above the number of elements, it is inserted at the end.
      • addElement

        public void addElement​(int obj)
        Adds an int to the end of the vector.
      • addElements

        public void addElements​(int[] elements)
        Appends an array of integers at the end of this vector.
        Parameters:
        elements - array of integers to be added to this vector.
        Since:
        TotalCross 1.2
      • removeElement

        public void removeElement​(int obj)
        Deletes the given integer from this vector.
      • removeAllElements

        public void removeAllElements()
        Sets all elements in this vector to 0 and its size to 0.
      • qsort

        public void qsort()
        Does a quick sort in the elements of this IntVector
      • toIntArray

        public int[] toIntArray()
        Returns a new array with the added elements
        Since:
        SuperWaba 5.54
      • setSize

        public void setSize​(int newSize)
                     throws java.lang.ArrayIndexOutOfBoundsException
        Sets the size of this vector. If the new size is greater than the current size, new items are added to the end of the vector. If the new size is less than the current size, all components at index newSize and greater are discarded.
        Parameters:
        newSize - the new size of this vector
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the new size is negative
        Since:
        TotalCross 1.0
      • copyInto

        public void copyInto​(int[] out)
        Copies the items of this vector into the given array, which must have at least the current size of this vector. If the out vector is greater than the current size, the remaining positions will remain unchanged.
        Since:
        TotalCross 1.0 beta 4
      • contains

        public boolean contains​(int v)
        Returns true if this vector contains the specified element.
        Parameters:
        v - element whose presence in this vector is to be tested
        Returns:
        true if this vector contains the specified element
        Since:
        TotalCross 1.15
      • reverse

        public void reverse()
        Reverses the order of the elements in this vector.
        In a vector with n elements, the element of index 0 is moved to the index n-1, the element of index 1 is moved to the index n-2, and so on.
        Since:
        TotalCross 1.15