Appearance
Java Vector
What exactly is a Java Vector?
Think of it as a dynamic array that automatically grows and shrinks as you add or remove elements. It's like having a magical backpack that expands to fit all your treasures and contracts when you need a little less.
Creating a Vector
Creating a Vector is as easy as. You simply declare it and start adding elements:
java
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<String> myVector = new Vector<>();
myVector.add("Apple");
myVector.add("Banana");
myVector.add("Orange");
System.out.println("My vector: " + myVector);
}
}
Dynamic Resizing
One of the coolest things about Java Vector is its ability to resize dynamically. So, even if you start with a small size, it can grow to accommodate more elements without breaking a sweat:
java
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<Integer> myVector = new Vector<>(3); // Start with initial capacity of 3
myVector.add(1);
myVector.add(2);
myVector.add(3);
myVector.add(4); // No problem, Vector grows automatically
System.out.println("My vector: " + myVector);
}
}
Synchronization
Another cool feature of Java Vector is its built-in synchronization. This means it's safe to use in multithreaded environments without worrying about data corruption:
java
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<Integer> myVector = new Vector<>();
myVector.add(1);
myVector.add(2);
myVector.add(3);
// Thread 1
new Thread(() -> {
synchronized (myVector) {
System.out.println("Thread 1: " + myVector);
}
}).start();
// Thread 2
new Thread(() -> {
synchronized (myVector) {
System.out.println("Thread 2: " + myVector);
}
}).start();
}
}
Iterating Over Elements
Iterating over the elements of a Vector is a piece of cake. You can use a simple for loop or enhanced for loop:
java
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<String> myVector = new Vector<>();
myVector.add("Apple");
myVector.add("Banana");
myVector.add("Orange");
// Using for loop
System.out.println("Using for loop:");
for (int i = 0; i < myVector.size(); i++) {
System.out.println(myVector.get(i));
}
// Using enhanced for loop
System.out.println("Using enhanced for loop:");
for (String fruit : myVector) {
System.out.println(fruit);
}
}
}
Methods in Vector
Method | Description |
---|---|
void addElement(Object obj) | Adds the specified component to the end of this vector, increasing its size by one. |
boolean removeElement(Object obj) | Removes the first occurrence of the specified element in this vector, if it is present. |
Object remove(int index) | Removes the element at the specified position in this vector. |
void removeAllElements() | Removes all components from this vector and sets its size to zero. |
void clear() | Removes all of the elements from this vector. |
Object elementAt(int index) | Returns the component at the specified index. |
void setElementAt(Object obj, int index) | Sets the component at the specified index of this vector to be the specified object. |
int size() | Returns the number of components in this vector. |
boolean isEmpty() | Tests if this vector has no components. |
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 capacity() | Returns the current capacity of this vector. |
void trimToSize() | Trims the capacity of this vector to be the vector's current size. |
void setSize(int newSize) | Sets the size of this vector. |
Enumeration<E> elements() | Returns an enumeration of the components of this vector. |
boolean contains(Object o) | Returns true if this vector contains the specified element. |
int indexOf(Object o) | Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element. |
int lastIndexOf(Object o) | Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element. |
Object clone() | Returns a shallow copy of this vector. |
void copyInto(Object[] anArray) | Copies the components of this vector into the specified array. |
String toString() | Returns a string representation of this vector. |
void setSize(int newSize) | Sets the size of this vector. |
void add(int index, E element) | Inserts the specified element at the specified position in this vector. |
E remove(int index) | Removes the element at the specified position in this vector and returns the removed element. |
void clear() | Removes all of the elements from this vector. |