Package totalcross.util
Class IntVector
- java.lang.Object
-
- totalcross.util.IntVector
-
public class IntVector extends java.lang.ObjectAn 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 thepublic array. Please use the add and remove methods to manipulate the IntVector.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddElement(int obj)Adds an int to the end of the vector.voidaddElements(int[] elements)Appends an array of integers at the end of this vector.booleancontains(int v)Returns true if this vector contains the specified element.voidcopyInto(int[] out)Copies the items of this vector into the given array, which must have at least the current size of this vector.voidensureBit(int index)Useful method to use when this IntVector will act like a bit vector, through the methodsisBitSetandsetBit.intindexOf(int elem)Finds the index of the given int.intindexOf(int elem, int index)Finds the index of the given int.voidinsertElementAt(int obj, int index)Inserts the object at the given index.booleanisBitSet(int index)Used to let this int vector act like a bit vector.booleanisEmpty()Returns if this IntVector is empty.intpeek()Returns the last int, without removing it.intpop()Returns the last int, removing it.voidpop(int howMany)Pops the given number of elements from this vector.voidpush(int obj)Pushes an int.voidqsort()Does a quick sort in the elements of this IntVectorvoidremoveAllElements()Sets all elements in this vector to 0 and its size to 0.voidremoveElement(int obj)Deletes the given integer from this vector.voidremoveElementAt(int index)Deletes the int 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.voidsetBit(int index, boolean on)Used to let this int vector act like a bit vector.voidsetSize(int newSize)Sets the size of this vector.intsize()Returns the number of ints in the vector.int[]toIntArray()Returns a new array with the added elements
-
-
-
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 methodsisBitSetandsetBit. 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
-
pop
public int pop() throws ElementNotFoundExceptionReturns the last int, removing it.- Throws:
ElementNotFoundException- When the stack is empty.
-
peek
public int peek() throws ElementNotFoundExceptionReturns the last int, without removing it.- Throws:
ElementNotFoundException- When the stack is empty.
-
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.ArrayIndexOutOfBoundsExceptionSets 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
-
-