Difference between ArrayList and Vector

Both ArrayList and Vector are classes in Java that implement dynamic arrays. However, there are some differences between the two:

  1. Synchronization: Vector is synchronized, meaning that multiple threads cannot access a Vector instance at the same time. On the other hand, ArrayList is not synchronized.
  2. Performance: Because Vector is synchronized, it can be slower than ArrayList, especially when dealing with a large number of elements.
  3. Resize: When an element is added to a Vector and the underlying array is already full, Vector automatically increases the array size by a certain amount. ArrayList, on the other hand, increases the size of its underlying array by 50% of the current size, which can be more efficient when adding elements.
  4. Iterator: Vector has two types of iterators, Enumeration and Iterator. ArrayList only has one type of iterator, which is the Iterator.

In summary, if thread safety is not a concern, and performance is a priority, ArrayList is usually preferred over Vector. If thread safety is a concern or you need to use Enumeration to traverse the list, then Vector may be a better choice.

Example of Java ArrayList:

Sure! Here’s an example of how to create and use an ArrayList in Java:

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // Create an empty ArrayList
        ArrayList<String> names = new ArrayList<>();

        // Add elements to the ArrayList
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        // Get the size of the ArrayList
        System.out.println("Size of the ArrayList: " + names.size());

        // Access an element in the ArrayList
        System.out.println("Element at index 1: " + names.get(1));

        // Modify an element in the ArrayList
        names.set(2, "David");

        // Remove an element from the ArrayList
        names.remove(0);

        // Loop through the ArrayList
        for (String name : names) {
            System.out.println(name);
        }

        // Check if an element is present in the ArrayList
        boolean isPresent = names.contains("Bob");
        System.out.println("Is Bob present? " + isPresent);
    }
}

In this example, we create an ArrayList of String type and add three elements to it. We then get the size of the ArrayList, access an element at a specific index, modify an element, remove an element, loop through the ArrayList to print each element, and check if a specific element is present in the ArrayList.

Example of Java Vector:

Certainly! Here’s an example of how to create and use a Vector in Java:

import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        // Create a Vector with an initial capacity of 3
        Vector<Integer> numbers = new Vector<>(3);

        // Add elements to the Vector
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        // Get the size of the Vector
        System.out.println("Size of the Vector: " + numbers.size());

        // Access an element in the Vector
        System.out.println("Element at index 1: " + numbers.get(1));

        // Modify an element in the Vector
        numbers.set(2, 40);

        // Remove an element from the Vector
        numbers.remove(0);

        // Loop through the Vector
        for (int number : numbers) {
            System.out.println(number);
        }

        // Check if an element is present in the Vector
        boolean isPresent = numbers.contains(20);
        System.out.println("Is 20 present? " + isPresent);
    }
}

In this example, we create a Vector of Integer type with an initial capacity of 3, and add three elements to it. We then get the size of the Vector, access an element at a specific index, modify an element, remove an element, loop through the Vector to print each element, and check if a specific element is present in the Vector.

Note that the methods used to manipulate the Vector are similar to those used for ArrayList, but with the added thread safety provided by synchronization.