Class Vector

  • Direct Known Subclasses:
    Node

    public class Vector
    extends java.lang.Object
    A vector is an array of object references. The vector grows dynamically as objects are added, but it never shrinks. The Vector class can also be used as a Stack class.

    Here is an example showing a vector being used:

     ...
     Vector vec = new Vector();
     vec.addElement(obj1);
     vec.addElement(obj2);
     ...
     vec.insertElementAt(obj3, 3);
     vec.removeElementAt(2);
     if (vec.size() > 5)
     ...
     
    This Vector class does not support Generics; use the ArrayList class instead.
    • Field Summary

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

      Constructors 
      Constructor Description
      Vector()
      Constructs an empty vector.
      Vector​(int size)
      Constructs an empty vector with a given initial size, which is the initial size of the vector's internal object array.
      Vector​(java.lang.Object[] startingWith)
      Constructs a vector starting with the given elements.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addElement​(java.lang.Object obj)
      Adds an object to the end of the vector.
      void addElements​(java.lang.Object[] objects)
      Adds an array of objects at the end of the vector.
      void addElementsNotNull​(java.lang.Object[] objects)
      Adds an array of objects at the end of the vector (null objects are skipped).
      boolean contains​(java.lang.Object o)
      Returns true if this vector contains the specified element.
      void copyInto​(java.lang.Object[] out)
      Copies the items of this vector into the given array, which must have at least the current size of this vector.
      java.lang.String dump()
      Dumps the contents of this vector and returns a string of it.
      java.lang.Object elementAt​(int i)  
      void ensureCapacity​(int minCapacity)
      Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
      int indexOf​(java.lang.Object elem)
      Finds the index of the given object.
      int indexOf​(java.lang.Object obj, int startIndex)
      Finds the index of the given object.
      void insertElementAt​(java.lang.Object obj, int index)
      Inserts the object at the given index.
      boolean isEmpty()
      Returns if this Vector is empty.
      java.lang.Object peek()
      Returns the last object, without removing it.
      java.lang.Object peek​(int n)
      Returns the n-last object, without removing it.
      java.lang.Object peek​(java.lang.Object def)
      Returns the last object, without removing it, or the given default value.
      java.lang.Object pop()
      Returns the last object, removing it.
      void pop​(int n)
      Pops n last elements from the stack.
      java.lang.Object pop​(java.lang.Object def)
      Returns the last object, removing it, or the given default value
      void push​(java.lang.Object obj)
      Pushes an object.
      void qsort()
      Sorts the elements of this Vector.
      void qsort​(boolean ascending)
      Sorts the elements of this Vector, in the given order.
      void qsort​(int sortType)
      Sorts the elements of this Vector, with the given sort type.
      void qsort​(int sortType, boolean ascending)
      Sorts the elements of this Vector, with the given sort type and order.
      void removeAllElements()
      Clears all elements in this vector and sets the count to 0.
      boolean removeElement​(java.lang.Object obj)
      Deletes the object.
      void removeElementAt​(int index)
      Deletes the object 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 setSize​(int newSize)
      Sets the size of this vector.
      int size()
      Returns the number of objects in the vector.
      java.lang.Object[] toObjectArray()
      Converts the vector to an array of objects.
      java.lang.String toString()
      Returns the items of this vector separated by comma
      java.lang.String toString​(java.lang.String separator)
      Returns the items of this vector separated by the given string.
      • Methods inherited from class java.lang.Object

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

      • items

        public java.lang.Object[] 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

      • Vector

        public Vector()
        Constructs an empty vector.
      • Vector

        public Vector​(int size)
        Constructs an empty vector with a given initial size, which is the initial size of the vector's internal object array. The vector will grow as needed when objects are added.
      • Vector

        public Vector​(java.lang.Object[] startingWith)
        Constructs a vector starting with the given elements. The vector can grow after this. Note that the given array must not have null elements.
        Parameters:
        startingWith -
    • Method Detail

      • toObjectArray

        public java.lang.Object[] toObjectArray()
        Converts the vector to an array of objects. If there are no elements in this vector, returns null. Note that if the elements are Strings, you can cast the result to a String[] array.
      • push

        public void push​(java.lang.Object obj)
        Pushes an object.
      • peek

        public java.lang.Object peek​(int n)
                              throws ElementNotFoundException
        Returns the n-last object, without removing it. Note that peek(0) is the same of peek().
        Parameters:
        n - How many elements to get from the top; must be a positive number.
        Throws:
        ElementNotFoundException - When the stack is empty.
      • pop

        public void pop​(int n)
        Pops n last elements from the stack.
      • pop

        public java.lang.Object pop​(java.lang.Object def)
        Returns the last object, removing it, or the given default value
      • peek

        public java.lang.Object peek​(java.lang.Object def)
        Returns the last object, without removing it, or the given default value.
      • isEmpty

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

        public int size()
        Returns the number of objects in the vector.
      • 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 null 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 beta 5
      • indexOf

        public int indexOf​(java.lang.Object elem)
        Finds the index of the given object. The list is searched using a O(n) linear search through all the objects in the vector.
      • indexOf

        public int indexOf​(java.lang.Object obj,
                           int startIndex)
        Finds the index of the given object. The list is searched using a O(n) linear search starting in startIndex up through all the objects in the vector.
      • removeElementAt

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

        public void insertElementAt​(java.lang.Object 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​(java.lang.Object obj)
        Adds an object to the end of the vector.
      • addElements

        public void addElements​(java.lang.Object[] objects)
        Adds an array of objects at the end of the vector.
        Parameters:
        objects - array with the objects to be added to the vector.
        Since:
        TotalCross 1.13
      • addElementsNotNull

        public void addElementsNotNull​(java.lang.Object[] objects)
        Adds an array of objects at the end of the vector (null objects are skipped).
        Parameters:
        objects - array with the objects to be added to the vector.
        Since:
        TotalCross 1.24
      • removeElement

        public boolean removeElement​(java.lang.Object obj)
        Deletes the object.
      • removeAllElements

        public void removeAllElements()
        Clears all elements in this vector and sets the count to 0. Note that this method sets all items in this vector to null, so, if you had directly assigned an array to this vector, all items inside it will be nulled.
      • qsort

        public void qsort()
        Sorts the elements of this Vector. If they are Strings, the sort will be much faster because a cast to String is done; if they are not strings, Convert.qsort will try to discover the type.
      • qsort

        public void qsort​(int sortType)
        Sorts the elements of this Vector, with the given sort type.
      • qsort

        public void qsort​(boolean ascending)
        Sorts the elements of this Vector, in the given order. If they are Strings, the sort will be much faster because a cast to String is done; if they are not strings, Convert.qsort will try to discover the type.
      • qsort

        public void qsort​(int sortType,
                          boolean ascending)
        Sorts the elements of this Vector, with the given sort type and order.
      • dump

        public java.lang.String dump()
        Dumps the contents of this vector and returns a string of it. If the number of elements is big, it can take a lot of memory!
      • copyInto

        public void copyInto​(java.lang.Object[] 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
      • ensureCapacity

        public void ensureCapacity​(int minCapacity)
        Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
        Parameters:
        minCapacity - the desired minimum capacity
      • toString

        public java.lang.String toString()
        Returns the items of this vector separated by comma
        Overrides:
        toString in class java.lang.Object
        Since:
        TotalCross 1.13
      • toString

        public java.lang.String toString​(java.lang.String separator)
        Returns the items of this vector separated by the given string.
        Since:
        TotalCross 1.13
      • contains

        public boolean contains​(java.lang.Object o)
        Returns true if this vector contains the specified element.
        Parameters:
        o - 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
      • elementAt

        public java.lang.Object elementAt​(int i)