What is Vector?
The Vector class implements a growable array of objects.
Similar to array, elements of Vector can be accessed using an integer index.
However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
How to perform Basic Operations in Vector?
The Basic Operations are:
- create vector object
- adding elements to it
- getting elements by specifying index
- getting elements index
- getting first element
- getting last element
- is vector is empty or not
Basic Operations
import java.util.Vector; public class BasicVectorOperations { public static void main(String a[]){ VectorTry it Yourselfv1 = new Vector (); //adding elements to the end v1.add("First"); v1.add("Second"); v1.add("Third"); System.out.println(v1); //adding element at specified index v1.add(2,"Random"); System.out.println(v1); //getting elements by index System.out.println("Element at index 3 is: "+v1.get(3)); //getting first element System.out.println("The first element of this vector is: "+v1.firstElement()); //getting last element System.out.println("The last element of this vector is: "+v1.lastElement()); //how to check vector is empty or not System.out.println("Is this vector empty? "+v1.isEmpty()); } }
How to read all elements in vector by using iterator?
By calling iterator() method you will get Iterator object, through which you can iterate through all the elements of the vector.
Example
import java.util.Iterator; import java.util.Vector; public class VectorIterator { public static void main(String a[]){ VectorTry it Yourselfv1 = new Vector (); //adding elements to the end v1.add("First"); v1.add("Second"); v1.add("Third"); v1.add("Random"); Iterator itr = v1.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
How to add all elements of a list to vector?
we have created an ArrayList and by using addAll() method, we can copy another collection object.
Example
import java.util.ArrayList; import java.util.List; import java.util.Vector; public class MyVectorNewCollection { public static void main(String a[]){ VectorTry it Yourselfv1 = new Vector (); //adding elements to the end v1.add("First"); v1.add("Second"); v1.add("Third"); v1.add("Random"); System.out.println("Actual vector:"+v1); List list = new ArrayList (); list.add("one"); list.add("two"); v1.addAll(list); System.out.println("After Copy: "+v1); } }
How to delete all elements from my vector?
We can call clear() method to delete all elements of a vector at one call.
Example
import java.util.Vector; public class ClearMyVector { public static void main(String a[]){ VectorTry it Yourselfv1 = new Vector (); //adding elements to the end v1.add("First"); v1.add("Second"); v1.add("Third"); v1.add("Random"); System.out.println("Actual vector:"+v1); v1.clear(); System.out.println("After clear vector:"+v1); } }
How to copy vector to array?
By passing an array object to copyInto() method, we can copy content of a vector object to an array.
Example
import java.util.Vector; public class MyVectorArrayCopy { public static void main(String a[]){ VectorTry it Yourselfv1 = new Vector (); v1.add("First"); v1.add("Second"); v1.add("Third"); v1.add("Random"); System.out.println("Actual vector:"+v1); String[] copyArr = new String[v1.size()]; v1.copyInto(copyArr); System.out.println("Copied Array content:"); for(String str:copyArr){ System.out.println(str); } } }
Solve this!!
#20099,20100,20101,20102- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling