Vector in java
Sent by Copilot:
Vector in Java Collection
The Vector
class in Java is part of the java.util
package and implements the List
interface. It is similar to ArrayList
but with some differences, primarily related to synchronization.
Key Characteristics of Vector
- Resizable Array: Like
ArrayList
,Vector
implements a dynamic array that can grow or shrink as needed. - Synchronization: All methods in
Vector
are synchronized, making it thread-safe. This means that only one thread can access aVector
object at a time, which can be useful in multi-threaded environments but may lead to performance overhead. - Legacy Class:
Vector
is considered a legacy class, but it is fully compatible with the collections framework.
Creating a Vector
You can create a Vector
in several ways:
Java
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
// Default constructor
Vector<Integer> vector1 = new Vector<>();
// Specifying initial capacity
Vector<Integer> vector2 = new Vector<>(10);
// Specifying initial capacity and capacity increment
Vector<Integer> vector3 = new Vector<>(10, 5);
}
}
Adding Elements to a Vector
You can add elements to a Vector
using the add
method:
Java
Accessing Elements
You can access elements using the get
method or an iterator:
Java
import java.util.Vector;
import java.util.Iterator;
public class AccessElements {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Orange");
// Using get method
String element = vector.get(1);
System.out.println("Element at index 1: " + element);
// Using iterator
Iterator<String> iterator = vector.iterator();
System.out.print("Vector elements: ");
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
}
}
Removing Elements
You can remove elements using the remove
method:
Java
import java.util.Vector;
public class RemoveElements {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Orange");
vector.remove(1); // Removing element at index 1
vector.remove("Orange"); // Removing specific element
System.out.println("Vector after removal: " + vector);
}
}
Example: Using Vector in a Real-World Scenario
Consider a scenario where you need to manage a list of students in a classroom. You can use a Vector
to store and manipulate this list:
Java
import java.util.Vector;
class Student {
private String name;
private int rollNumber;
public Student(String name, int rollNumber) {
this.name = name;
this.rollNumber = rollNumber;
}
@Override
public String toString() {
return "Student{name='" + name + "', rollNumber=" + rollNumber + "}";
}
}
public class Classroom {
public static void main(String[] args) {
Vector<Student> students = new Vector<>();
// Adding students
students.add(new Student("Alice", 1));
students.add(new Student("Bob", 2));
students.add(new Student("Charlie", 3));
// Displaying students
System.out.println("Students in the classroom:");
for (Student student : students) {
System.out.println(student);
}
// Removing a student
students.remove(1); // Removing the student at index 1
// Displaying students after removal
System.out.println("Students after removal:");
for (Student student : students) {
System.out.println(student);
}
}
}
ArrayIndexOutOfBound example:
Vector<Integer> vec = new Vector<Integer>();
vec.add(0,11);
vec.add(1,22);
vec.add(3, 33); //THIS WILL GIVE ARRAYINDEXOUTOFBOUNDEXCEPTION
vec.add(1,44); //This will move the existing element at 1th position to the next position and this new element will be added to the first index.
Comments
Post a Comment