vector class in java program
introduction vector class in java:
The Vector category uses a lot of things that can grow. Vectors basically fall into legacy classes but now work entirely with collections. It is available in the Java.util package and is applicable to the list interface interface, so we can use all the list display methods here.
Vector is like a flexible collection that can increase or decrease in size. Unlike similar members, we can store n-number items in them as there is no size limit. It is part of the framework for Java collection since Java 1.2. It is available in the Java.util package and is applicable to the list interface interface, so we can use all the list display methods here.
It is recommended that the Vector section be used for safe use of the cord only. If you do not need to use a secure cable implementation, you should use ArrayList, ArrayList will work best in such a situation.
Iterators retrieved by the Vector phase fail quickly. In the event of a similar conversion, it fails and throws ConcurrentModificationException.
Example vector class:
import java.util.*;
public class VectorExample {
public static void main(String args[]) {
//Create a vector
Vector<String> vec = new Vector<String>();
//Adding elements using add() method of List
vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");
//Adding elements using addElement() method of Vector
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
System.out.println("Elements are: "+vec);
}
}
Vector uses a powerful list which means it can grow or shrink as needed. Like the same members, it contains material that is available using a complete index
They are very similar to ArrayList but Vector is synchronized and has some legacy method that the collection framework does not contain.
It also maintains an insertion order like an ArrayList but it is rarely used in a non-thread environment as it is synchronized and due to which it gives a poor performance in adding, searching, delete and update of its elements.
The Iterators returned by the Vector class are fail-fast. In the case of concurrent modification, it fails and throws the ConcurrentModificationException.
What is vector in Java with example?
Vector is synchronized. Java Vector contains many legacy methods that are not the part of a collections framework.
vector and arraylist in java:
ArrayList and Vectors both use an interactive interface and both use (dynamic flexibility) internal data editing, much like using a standard list.
What is difference vector and ArrayList?
ArrayList is non-synchronized. Vector is synchronized. ArrayList increments 50% of its current size if element added exceeds its capacity. Vector increments 100% of its current size if element added exceeds its capacity.
vector and arraylist example:
import java.io.*;
import java.util.*;
class GFG
{
public static void main (String[] args)
{
// creating an ArrayList
ArrayList<String> al = new ArrayList<String>();
// adding object to arraylist
al.add("Learn");
al.add("To");
al.add("Java");
al.add("vector class");
// traversing elements using Iterator'
System.out.println("ArrayList elements are:");
Iterator it = al.iterator();
while (it.hasNext())
System.out.println(it.next());
// creating Vector
Vector<String> v = new Vector<String>();
v.addElement("learn");
v.addElement("Java");
v.addElement("Vectore");
// traversing elements using Enumeration
System.out.println("\nVector elements are:");
Enumeration e = v.elements();
while (e.hasMoreElements())
System.out.println(e.nextElement());
}
}
No comments:
Post a Comment