Vector in Java with examples and explanation

In this article, we will learn some basic concepts on vector in Java and also provide some examples. We will implement our idea with the help of Java. At first, we will discuss preliminary ideas on vectors and then move to the main part.

What is Vector?

A vector is an object which has both direction and magnitude. Suppose, we have a vector called AB. If AB starts with A, then A is called an initial point and B is called the terminal point. The distance between the initial point and the terminal is called the magnitude of a vector. The direction of this object can be obtained by its arrow.

Therefore, we are creating this object using the fundamentals of object-oriented programming language i.e. java.

Creating a Vector in Java

Below is the Java syntax that shows how we can create our vector:

Vector<Integer> vector = new Vector<>();

Here, we have used the vector class to create a new object. Now, it has many methods to add elements, remove an element, access the elements etc.

Add Elements

  • add(item)= add one item
  • add(position, item)= add the item to the specified position
  • addAll(vector)= add all the elements of one vector to another vector.
import java.util.Vector;

class Addelements {
    public static void main(String[] args) {
        Vector<String> items= new Vector<>();

        // Using the add() method
        items.add("abc");
        items.add("xyz");

        // Using index number
        items.add(2, "pqr");
        System.out.println("Vector: " + items);

        // Using addAll()
        Vector<String> box = new Vector<>();
        box.add("jkl");

        box.addAll(items);
        System.out.println("New Vector: " + box);
    }
}

OUTPUT:

Vector: [abc, xyz, pqr]
New Vector: [jkl, abc, xyz, pqr]

Explanation:

At first, we have created the object using vector class. Therefore, we use the add() method to add elements into this. Then we gave the index number to add the element in the particular position. Here, we have provided the position number 2 to add the item. After that, we have used the addAll() method to add all the elements of “items” to “box”.

Remove Elements

  • remove(position) = deletes the item from that particular position
  • removeAll() = deletes all the elements
  • clear() = removes all the elements but it is more efficient than removeAll() method
import java.util.Vector;

class remove_elements {
    public static void main(String[] args) {
        Vector<String> items= new Vector<>();
        items.add("abc");
        items.add("xyz");
        items.add("efg");

        System.out.println("Initial Vector: " + items);

        // Using remove()
        String element = items.remove(1);
        System.out.println("Removed Element: " + element);
        System.out.println("New Vector: " + items);

        // Using clear()
        items.clear();
        System.out.println("Vector after clear(): " + items);
    }
}

OUTPUT:

Initial Vector: [abc, xyz, efg]
Removed Element: xyz
New Vector: [abc, efg]
Vector after clear(): []

Explanation:

At first, we have created the object and added items simultaneously. Then, we provide the position of the item which has to be removed. Therefore, we have used the clear() method to remove all the items of the object.

Access Elements

  • get(index) = shows the element of this particular position
  • iterator() = returns the object sequentially
import java.util.Iterator;
import java.util.Vector;

class access {
    public static void main(String[] args) {
        Vector<String> items= new Vector<>();
        items.add("abc");
        items.add("def");
        items.add("pqr");

        // Using get()
        String element = items.get(2);
        System.out.println("Element at index 2: " + element);

        // Using iterator()
        Iterator<String> iterate = items.iterator();
        System.out.print("Vector: ");
        while(iterate.hasNext()) {
            System.out.print(iterate.next());
            System.out.print(", ");
        }
    }
}

OUTPUT:

Element at index 2: pqr
Vector: abc, def, pqr

Explanation

At first, we have generated the object with items and then accessed in the specified position. Here, we gave the position number 2 and then we got the output “par”. After that, we have used the iterator() method to show all the items inside the object.

For details, you can go through this article.

Leave a Reply

Your email address will not be published. Required fields are marked *