티스토리 뷰

Programming Language/JAVA

[Java] Vector

나수비니 2023. 10. 10. 15:22
728x90

Vector Class in Java

 

package twoOct;
import java.util.Vector;

public class vectorTest {
	public static void main(String[] args) {
		
		//Size of the Vector
		int n = 5;
		
		//Declaring the Vector with
		//initial size n
		Vector<Integer> v = new Vector<Integer>(n);
		
		//Appending new elements at the end of the vector
		for (int i = 1; i <= n; i++)
			v.add(i);
		
		System.out.println(v);
		
		v.remove(3);
		
		System.out.println(v);
		
		// Using set() method to replace with 2 with 77
		System.out.print("The object that is replaced is: " +  v.set(0,77));
		
		//iterating over vector elements using for loop
		for (int i = 0; i < v.size(); i++)
			System.out.print(v.get(i)+ " ");
	}
}

 

Updating Elements

If I wish to change the elements, it can be done using the set() method. Since a Vector is indexed, the element which I wish to change is referenced by the index of the element.

Therefore, this method takes an index and the updated element to be inserted at that index.

 

// Using set() method to replace with 2 with 77
		System.out.print("The object that is replaced is: " +  v.set(0,77));

 

Removing Elements

In order to remove an element from a Vector, I can use the remove() method. This method is overloaded to perform multiple operations based on different parameters.

  • remove(Object) : This method is used to remove an object from the Vector. If multiple such objects, then the first occurrence of the object is removed.
  • remove(int index) : Since a Vector is indexed, this method takes an integer value which simply removes the element present at that specific index in the Vector.

 

package twoOct;

import java.util.Vector;

public class RemovingElementsFromVector {

	public static void main(String[] args) {

		//create default vector of capacity 10
		Vector v = new Vector();
		
		// Add elements using add() method
		v.add(1);
		v.add(2);
		v.add("Geeks");
		v.add(4);
		
		//removing first occurrence element at 1
		v.remove(1);
		
		//checking vector
		System.out.println("after removal: " + v);
	}
}

 

Iterating the Vector

There are multiple ways to iterate through the Vector. The most famous ways are by using the basic for loop in combination with a get() method to get the element at a specific index and the advanced for a loop.

 

package Vector;
import java.util.Vector;

public class IteratingVector {
	public static void main(String[] args) {

		//create an instance of vector
		Vector<String> v = new Vector<>();
		
		//add elements using add() method
		v.add("Geeks");
		v.add("Geeks");
		v.add(2, "For");
		
		for (int i = 0; i < v.size(); i++) {
			System.out.print(v.get(i) + " ");
		}
		
		System.out.println();
		
		//Using the for each loop
		
		for (String str : v)
			System.out.print(str + " ");
	}
}

 

A simple example that demonstrates how to use a Vector in Java

 

package Vector;

import java.util.Vector;

public class VectorExample {

	public static void main(String[] args) {

		//create a new vector
		Vector<Integer> v = new Vector<>(3,2);
		
		//add elements to the vector
		v.addElement(1);
		v.addElement(2);		
		v.addElement(3);
		
		//insert an element at index 1
		v.insertElementAt(0, 1);
		
		System.out.println(v);
		
		//remove the element at index 2
		v.removeElementAt(2);
		
		//print the elements of the vector
		for (int i : v) {
			System.out.println(i);
		}
	}
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함