6.1 Differences Between Vectors and Arrays in Java
6.1 Differences Between Vectors and Arrays in Java
Java
In the world of Java programming, data structures play a crucial role in
storing and manipulating data efficiently. Two commonly used data
structures for this purpose are vectors and arrays. While both are used to
store collections of elements, they have distinct differences that make
them suitable for different scenarios. In this section, we will explore the
dissimilarities between vectors and arrays in Java.
Java Array
An array in Java is a fixed-size, homogeneous data structure that stores
elements of the same type. Arrays have a static length, meaning the size
is determined at the time of declaration and cannot be changed during
runtime.
They offer direct access to elements using an index, which makes array
operations fast and efficient.
Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Java Vector
A vector is a dynamic and resizable array-like data structure that belongs
to the Java Collections Framework. Vectors can grow or shrink in size
dynamically, allowing for flexibility in managing the number of elements
they contain.
Vectors are part of the java.util package and implement the List interface,
providing various methods for manipulation. Let's go through some simple
examples for both arrays and vectors in Java to illustrate their differences.
1. import java.util.Vector;
2. public class VectorExample {
3. public static void main(String[] args) {
4. // Declaration and initialization of a Vector
5. Vector<Integer> integerVector = new Vector<>();
6. // Adding elements to the Vector
7. integerVector.add(1);
8. integerVector.add(2);
9. integerVector.add(3);
10. // Accessing vector elements
11. for (int i = 0; i < integerVector.size(); i++) {
12. System.out.println("Element at index " + i + ": " + inte
gerVector.get(i));
13. }
14. }
15. }
Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Comparison of Programs
In the array example, we declare and initialize an array with a fixed size.
We then assign values to its elements and access them using indices.
ADVERTISEMENT
In the vector example, we have used the Vector class from the java.util
package. Vectors are dynamic and can grow or shrink as needed. We add
elements using the add() method and access them using the get()
method.
Size Flexibility
Array
Once an array is created, its size remains constant, and we cannot change
it dynamically. To resize an array, we need to create a new array and copy
the elements from the old array to the new one.
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index
3 out of bounds for length 3
at ArrayResizeErrorExample.main(ArrayResizeErrorExample.java:12)
Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
Vector
Vectors are dynamic and can be resized during runtime using methods
like add() and remove(). The Vector class automatically handles the
resizing of the underlying array, providing a convenient way to manage
the size of the collection.
1. import java.util.Vector;
2. public class VectorExample {
3. public static void main(String[] args) {
4. // Declaration and initialization of a Vector
5. Vector<Integer> integerVector = new Vector<>();
6. // Adding elements to the Vector
7. integerVector.add(1);
8. integerVector.add(2);
9. integerVector.add(3);
10. // Resizing the Vector dynamically
11. integerVector.add(4);
12. integerVector.add(5);
13. // Accessing vector elements
14. for (int i = 0; i < integerVector.size(); i++) {
15. System.out.println("Element at index " + i + ": " + inte
gerVector.get(i));
16. }
17. }
18. }
Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
In this example, the Vector is dynamically resized by adding two more
elements to it after the initial elements have been added. Vectors can
grow or shrink in size during runtime, providing flexibility in managing the
number of elements they contain.
Synchronization
Array
Arrays in Java are not synchronized, meaning they are not thread-safe. If
multiple threads are accessing or modifying an array concurrently, we
may encounter data inconsistencies.
Output:
Element at index 0: 0
Element at index 1: 0
Element at index 2: 0
In this example, two threads concurrently modify the array without any
synchronization. As a result, data inconsistencies may occur, and the final
values of the array elements can vary.
Vector
Vectors are synchronized by default, ensuring that multiple threads can
safely access and modify them. While synchronization introduces
overhead, it can be beneficial in situations where thread safety is a
concern.
1. import java.util.Vector;
2. public class VectorThreadExample {
3. public static void main(String[] args) {
4. // Declaration and initialization of a Vector
5. Vector<Integer> integerVector = new Vector<>();
6. // Creating two threads that modify the vector concurrently
7. Thread thread1 = new Thread(() -> {
8. for (int i = 0; i < 3; i++) {
9. integerVector.add(i);
10. }
11. });
12. Thread thread2 = new Thread(() -> {
13. for (int i = 0; i < 3; i++) {
14. integerVector.remove(0);
15. }
16. });
17. // Start the threads
18. thread1.start();
19. thread2.start();
20. try {
21. // Wait for threads to complete
22. thread1.join();
23. thread2.join();
24. } catch (InterruptedException e) {
25. e.printStackTrace();
26. }
27. // Accessing vector elements after concurrent modificatio
n
28. for (int i = 0; i < integerVector.size(); i++) {
29. System.out.println("Element at index " + i + ": " + inte
gerVector.get(i));
30. }
31. }
32. }
Output:
Element at index 0: 2
Element at index 1: 1
Performance