Package totalcross.util
Class Vector
- java.lang.Object
-
- totalcross.util.Vector
-
- Direct Known Subclasses:
Node
public class Vector extends java.lang.ObjectA 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.
-
-
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 voidaddElement(java.lang.Object obj)Adds an object to the end of the vector.voidaddElements(java.lang.Object[] objects)Adds an array of objects at the end of the vector.voidaddElementsNotNull(java.lang.Object[] objects)Adds an array of objects at the end of the vector (null objects are skipped).booleancontains(java.lang.Object o)Returns true if this vector contains the specified element.voidcopyInto(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.Stringdump()Dumps the contents of this vector and returns a string of it.java.lang.ObjectelementAt(int i)voidensureCapacity(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.intindexOf(java.lang.Object elem)Finds the index of the given object.intindexOf(java.lang.Object obj, int startIndex)Finds the index of the given object.voidinsertElementAt(java.lang.Object obj, int index)Inserts the object at the given index.booleanisEmpty()Returns if this Vector is empty.java.lang.Objectpeek()Returns the last object, without removing it.java.lang.Objectpeek(int n)Returns the n-last object, without removing it.java.lang.Objectpeek(java.lang.Object def)Returns the last object, without removing it, or the given default value.java.lang.Objectpop()Returns the last object, removing it.voidpop(int n)Pops n last elements from the stack.java.lang.Objectpop(java.lang.Object def)Returns the last object, removing it, or the given default valuevoidpush(java.lang.Object obj)Pushes an object.voidqsort()Sorts the elements of this Vector.voidqsort(boolean ascending)Sorts the elements of this Vector, in the given order.voidqsort(int sortType)Sorts the elements of this Vector, with the given sort type.voidqsort(int sortType, boolean ascending)Sorts the elements of this Vector, with the given sort type and order.voidremoveAllElements()Clears all elements in this vector and sets the count to 0.booleanremoveElement(java.lang.Object obj)Deletes the object.voidremoveElementAt(int index)Deletes the object reference at the given index.voidreverse()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.voidsetSize(int newSize)Sets the size of this vector.intsize()Returns the number of objects in the vector.java.lang.Object[]toObjectArray()Converts the vector to an array of objects.java.lang.StringtoString()Returns the items of this vector separated by commajava.lang.StringtoString(java.lang.String separator)Returns the items of this vector separated by the given string.
-
-
-
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.
-
pop
public java.lang.Object pop() throws ElementNotFoundExceptionReturns the last object, removing it.- Throws:
ElementNotFoundException- When the stack is empty.
-
peek
public java.lang.Object peek() throws ElementNotFoundExceptionReturns the last object, without removing it.- Throws:
ElementNotFoundException- When the stack is empty.
-
peek
public java.lang.Object peek(int n) throws ElementNotFoundExceptionReturns the n-last object, without removing it. Note thatpeek(0)is the same ofpeek().- 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.ArrayIndexOutOfBoundsExceptionSets 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 tonull, 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:
toStringin classjava.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)
-
-