
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java Vs. C++
- JVM - Java Virtual Machine
- Java - JDK vs JRE vs JVM
- Java - Environment Setup
- Java - Hello World Program
- Java - Comments
- Java - Basic Syntax
- Java - Variables
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - User Input
- Java - Date & Time
Java Operators
- Java - Operators
- Java - Arithmetic Operators
- Java - Assignment Operators
- Java - Relational Operators
- Java - Logical Operators
- Java - Bitwise Operators
- Java Operator Precedence & Associativity
Java Control Statements
- Java - Decision Making
- Java - If Else Statement
- Java - Switch Statement
- Java - Loop Control
- Java - For Loop
- Java - For-Each Loop
- Java - While Loop
- Java - Do While Loop
- Java - Break Statement
- Java - Continue Statement
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner
- Java - Arrays
- Java - Strings
- Java - Date
- Java - ArrayList
- Java - Vector
- Java - Stack
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - IdentityHashMap
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection
- Java - Array
Java Useful Resources
Java - The Vector Class
Vector implements a dynamic array. It is similar to ArrayList, but with two differences −
Vector is synchronized.
Vector contains many legacy methods that are not part of the collections framework.
Vector proves to be very useful if you don't know the size of the array in advance or you just need one that can change sizes over the lifetime of a program.
Following is the list of constructors provided by the vector class.
Sr.No. | Constructor & Description |
---|---|
1 |
Vector( ) This constructor creates a default vector, which has an initial size of 10. |
2 |
Vector(int size) This constructor accepts an argument that equals to the required size, and creates a vector whose initial capacity is specified by size. |
3 |
Vector(int size, int incr) This constructor creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward. |
4 |
Vector(Collection c) This constructor creates a vector that contains the elements of collection c. |
Apart from the methods inherited from its parent classes, Vector defines the following methods −
Sr.No. | Method & Description |
---|---|
1 |
boolean add(E e)
This method appends the specified element to the end of this Vector. |
2 |
boolean addAll(Collection<? extends E> c)
This method appends all of the elements in the specified Collection to the end of this Vector. |
3 |
void addElement(E obj)
This method adds the specified component to the end of this vector, increasing its size by one. |
4 |
int capacity()
This method returns the current capacity of this vector. |
5 |
void clear()
This method removes all of the elements from this vector. |
6 |
clone clone()
This method returns a clone of this vector. |
7 |
boolean contains(Object o)
This method returns true if this vector contains the specified element. |
8 |
boolean containsAll(Collection<?> c)
This method returns true if this Vector contains all of the elements in the specified Collection. |
9 |
void copyInto(Object[ ] anArray)
This method copies the components of this vector into the specified array. |
10 |
E elementAt(int index)
This method returns the component at the specified index. |
11 |
Enumeration<E> elements()
This method returns an enumeration of the components of this vector. |
12 |
void ensureCapacity(int minCapacity)
This method increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument. |
13 |
boolean equals(Object o)
This method compares the specified Object with this Vector for equality. |
14 |
E firstElement()
This method returns the first component (the item at index 0) of this vector. |
15 |
void forEach(Consumer<? super E> action)
This method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
16 |
E get(int index)
This method returns the element at the specified position in this Vector. |
17 |
int hashCode()
This method returns the hash code value for this Vector. |
18 |
int indexOf(Object o)
This method returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element. |
19 |
void insertElementAt(E obj, int index)
This method inserts the specified object as a component in this vector at the specified index. |
20 |
boolean isEmpty()
This method tests if this vector has no components. |
21 |
Iterator<E&t; iterator()
This method returns an iterator over the elements in this list in proper sequence. |
22 |
E lastElement()
This method returns the last component of the vector. |
23 |
int lastIndexOf(Object o)
This method returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element. |
24 |
ListIterator<E> listIterator()
This method returns a list iterator over the elements in this list (in proper sequence). |
25 |
E remove(int index)
This method removes the element at the specified position in this Vector. |
26 |
boolean removeAll(Collection<?> c)
This method removes from this Vector all of its elements that are contained in the specified Collection. |
27 |
void removeAllElements()
This method removes all components from this vector and sets its size to zero. |
28 |
boolean removeElement(Object obj)
This method removes the first occurrence of the argument from this vector. |
29 |
void removeElementAt(int index)
This method deletes the component at the specified index. |
30 |
boolean removeIf(Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate. |
31 |
boolean retainAll(Collection<?> c)
This method retains only the elements in this Vector that are contained in the specified Collection. |
32 |
E set(int index, E element)
This method replaces the element at the specified position in this Vector with the specified element. |
33 |
void setElementAt(E obj, int index)
This method sets the component at the specified index of this vector to be the specified object. |
34 |
void setSize(int newSize)
This method sets the size of this vector. |
35 |
int size()
This method returns the number of components in this vector. |
36 |
Spliterator<E&t; spliterator()
Creates a late-binding and fail-fast Spliterator over the elements in this list. |
37 |
List <E> subList(int fromIndex, int toIndex)
This method returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. |
38 |
object[] toArray()
This method returns an array containing all of the elements in this Vector in the correct order. |
39 |
String toString()
This method returns a string representation of this Vector, containing the String representation of each element. |
40 |
void trimToSize()
This method trims the capacity of this vector to be the vector's current size. |
Example
The following program illustrates several of the methods supported by this collection −
import java.util.*; public class VectorDemo { public static void main(String args[]) { // initial size is 3, increment is 2 Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new Integer(2)); v.addElement(new Integer(3)); v.addElement(new Integer(4)); System.out.println("Capacity after four additions: " + v.capacity()); v.addElement(new Double(5.45)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Double(6.08)); v.addElement(new Integer(7)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Float(9.4)); v.addElement(new Integer(10)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Integer(11)); v.addElement(new Integer(12)); System.out.println("First element: " + (Integer)v.firstElement()); System.out.println("Last element: " + (Integer)v.lastElement()); if(v.contains(new Integer(3))) System.out.println("Vector contains 3."); // enumerate the elements in the vector. Enumeration vEnum = v.elements(); System.out.println("\nElements in vector:"); while(vEnum.hasMoreElements()) System.out.print(vEnum.nextElement() + " "); System.out.println(); } }
This will produce the following result −
Output
Initial size: 0 Initial capacity: 3 Capacity after four additions: 5 Current capacity: 5 Current capacity: 7 Current capacity: 9 First element: 1 Last element: 12 Vector contains 3. Elements in vector: 1 2 3 4 5.45 6.08 7 9.4 10 11 12