Java Vector Class – Explained with Methods and Example जावा वेक्टर क्लास – विधियों और उदाहरण सहित समझाया गया
Introduction to Vector Class वेक्टर क्लास का परिचय
The Vector class in Java is like a dynamic array that grows as needed. Unlike arrays, you don't need to specify its size in advance. It is a part of the java.util
package and implements the List
interface. Objects in a vector can be added, removed, accessed, or searched using their index positions.
वेक्टर क्लास, जावा में एक डायनामिक ऐरे की तरह होता है, जो आवश्यकता अनुसार आकार बढ़ा सकता है। पारंपरिक ऐरे के विपरीत, इसमें साइज पहले से तय करने की जरूरत नहीं होती। यह java.util
पैकेज का हिस्सा है और List
इंटरफ़ेस को इम्प्लीमेंट करता है। इसमें ऑब्जेक्ट्स को इंडेक्स द्वारा जोड़ा, हटाया, खोजा और एक्सेस किया जा सकता है।
🧾 Syntax / सिंटैक्स
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Here <E>
stands for Element type.
यहाँ <E>
का अर्थ है एलिमेंट (Element) का प्रकार।
🔧 Commonly Used Vector Methods | सामान्यत: प्रयोग होने वाले वेक्टर मेथड्स
Method | Description (English) | विवरण (हिंदी) |
---|---|---|
add() |
Adds an element to the vector | वेक्टर में एलिमेंट जोड़ता है |
addAll() |
Adds multiple elements at once | एक साथ कई एलिमेंट जोड़ता है |
firstElement() |
Returns the first element | पहले एलिमेंट को लौटाता है |
clear() |
Empties the vector | वेक्टर को खाली करता है |
get() |
Gets element at specific index | किसी विशेष इंडेक्स का एलिमेंट प्राप्त करता है |
insertElementAt() |
Inserts element at specific index | विशेष इंडेक्स पर एलिमेंट जोड़ता है |
indexOf() |
Finds index of given element | दिए गए एलिमेंट का इंडेक्स देता है |
remove() |
Removes element at a specific index | विशेष इंडेक्स से एलिमेंट हटाता है |
removeAllElements() |
Removes all elements | सभी एलिमेंट हटाता है |
clone() |
Creates a clone (copy) of vector | वेक्टर की क्लोन कॉपी बनाता है |
capacity() |
Returns current capacity of vector | वेक्टर की वर्तमान क्षमता बताता है |
contains() |
Checks if vector contains an element | जांचता है कि एलिमेंट मौजूद है या नहीं |
isEmpty() |
Checks if the vector is empty | वेक्टर खाली है या नहीं |
lastElement() |
Returns the last element in the vector | वेक्टर का अंतिम एलिमेंट लौटाता है |
💻 Example Program | उदाहरण प्रोग्राम
import java.util.*;
public class useOfVector {
public static void main(String args[]) {
Vector<String> v = new Vector<>();
v.addElement("ram");
v.addElement("syam");
v.addElement("raja");
v.addElement("rohan");
v.addElement("sohan");
System.out.println("This name vector contains: " + v.firstElement());
System.out.println("These name vector contains: " + v.lastElement());
System.out.println("Capacity of this vector: " + v.capacity());
System.out.println("Size of this vector: " + v.size());
for(int i = 0; i < v.size(); i++) {
System.out.println(" " + v.get(i));
}
}
}
🖨 Output | आउटपुट
This name vector contains: ram
These name vector contains: sohan
Capacity of this vector: 10
Size of this vector: 5
ram
syam
raja
rohan
sohan
📌 Key Points
-
Vector increases its capacity automatically.
-
Can hold different types of objects if not type-parameterized.
-
Thread-safe, hence slightly slower than
ArrayList
.
📌 मुख्य बातें
-
वेक्टर की क्षमता अपने आप बढ़ जाती है।
-
यदि जनरिक्स का उपयोग नहीं किया गया हो, तो यह विभिन्न प्रकार के ऑब्जेक्ट्स रख सकता है।
-
थ्रेड-सेफ होता है, इसलिए
ArrayList
की तुलना में थोड़ा धीमा होता है।
Comments
Post a Comment