티스토리 뷰
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);
}
}
}
'Programming Language > JAVA' 카테고리의 다른 글
[Java] Checking whether the Character is vowel or consonant. / 문자가 모음인지 자음인지 판별하기 (0) | 2023.10.10 |
---|---|
[Java] Modifier (1) | 2023.10.10 |
[Java] Exercise: Simple Calculator Program (자바로 간단한 계산기 구현하기) (0) | 2023.10.04 |
[Java] next(), nextLine() (0) | 2023.10.03 |
[Java] GUI 기초, Swing과 AWT (0) | 2023.10.02 |