Reference for Wiring version 0027+. If you have a previous version, use the reference included with your software. If you see any errors or have any comments, let us know.
Name |
Vector |
Examples |
Vector < int > intVector;
void setup() {
Serial.begin(9600);
pinMode(48, OUTPUT);
// turn ON wiring hardware LED
digitalWrite(48, HIGH);
// add 255 elements to the vector (from 0 to 254)
for(int i=0; i<255; i++) {
intVector.addElement(i);
}
Serial.print("The vector's capacity is: ");
// print the vector's capacity
Serial.println(intVector.capacity(), DEC);
// check if contains number 15
if(intVector.contains(15) == true) {
Serial.println("The vector contains the element 15");
}
Serial.print("The vector's first element is: ");
// print the vector's first element
Serial.println(intVector.firstElement(), DEC);
Serial.print("The vector's index for element 30 is: ");
// print the vector's element at position 30
Serial.println(intVector.indexOf(30), DEC);
// check if vector has elements
if(intVector.isEmpty() == false) {
Serial.println("The vector has elements");
}
Serial.print("The vector's last element is: ");
// print the vector's last element
Serial.println(intVector.lastElement(), DEC);
Serial.print("The vector's last index of 10 is: ");
// print the vector's last index of 10
Serial.println(intVector.lastIndexOf(10), DEC);
Serial.print("The vector's size is: ");
// print the vector's size
Serial.println(intVector.size(), DEC);
// if adding element is successful
if(intVector.add(255) == true) {
Serial.print("the element was added and now the vector's last element is: ");
// print the vector's last element
Serial.println(intVector.lastElement(), DEC);
}
intVector.addElement(256); // add another element
Serial.print("now the vector's last element is: ");
// print the vector's last element
Serial.println(intVector.lastElement(), DEC);
// insert a '0' at index 10
intVector.insertElementAt(0, 10);
int t = intVector.elementAt(10); // get the element at index 10
Serial.print("t is: ");
Serial.println(t, DEC); // print its value
intVector.remove(10); // remove element at position 10
t = intVector.get(10); // get the element at position 10
Serial.print("t after remove is: ");
Serial.println(t, DEC); // print the element at position 10
// remove element at position 10
intVector.removeElementAt(10);
// get the element at position 10
t = intVector.get(10);
// print the element at position 10
Serial.print("t after removeElementAt is: ");
Serial.println(t, DEC);
// remove element with value '23' from the Vector
if(intVector.removeElement(23)==true) {
Serial.println("the element 23 was removed.");
}
// set element at postion 5 with value 10
intVector.setElementAt(10, 5);
// get the element at position 10
t = intVector.get(5);
Serial.print("element at index 5 is now: ");
// print the element at position 10
Serial.println(t, DEC);
// clear all elements in the vector
intVector.clear();
Serial.print("The vector's size after clear is: ");
// print the vector's size
Serial.println(intVector.size(), DEC);
}
void loop() {
}
|
Description |
A vector is a growable array of elements. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. The class Vector includes methods for examining individual elements, comparing, searching and extracting parts.
Note that the Wiring Vector class has differences with the processing or Java Vector class. The main difference is that some methods modify the actual vector instead of returning a modified copy of it and it can only hold simple types of elements: int, long or char. |
Fields |
|
Methods |
capacity() |
Returns the current capacity of this vector |
contains() |
Tests if the specified element is a component in this vector |
firstElement() |
Returns the first element (the item at index 0) of this vector |
indexOf() |
Searches for the first occurence of the given element |
isEmpty() |
Tests if this vector has no elements |
lastElement() |
Returns the last element of the vector |
lastIndexOf() |
Returns the index value of the last occurrence of an element within the input element |
size() |
Returns the number of elements in this vector |
copyInto() |
Copies the components of this vector into the specified array |
add() |
Appends the specified element to the end of this vector |
addElement() |
Adds the specified element to the end of this vector, increasing its size by one |
clear() |
Removes all of the elements from this vector |
ensureCapacity() |
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 |
removeAllElements() |
Removes all elements from this vector and sets its size to zero |
setSize() |
Sets the size of this vector |
trimToSize() |
Trims the capacity of this vector to be the vector's current size |
elementAt() |
Returns the component at the specified index |
insertElementAt() |
Inserts the specified element as a component in this vector at the specified index |
remove() |
Removes the element at the specified position in this vector |
removeElementAt() |
Deletes the element at the specified index |
setElementAt() |
Sets the component at the specified index of this vector to be the specified element |
get() |
Returns the element at the specified position in this Vector |
|
Constructor |
Vector <datatype> variable
Vector()
Vector(data)
Vector(initialCapacity)
Vector(initialCapacity, capacityIncrement) |
Parameters |
datatype |
datatype: the type of elements the array can hold: int, long, char etc. |
data |
Vector: vector to be copied into the created vector |
initialCapacity |
the vector's specified initial capacity |
capacityIncrement |
the vector's specified initial capacity increment |
|
Usage |
Application |
Updated on September 17, 2010 01:04:52pm PDT