Java uses both ArrayList and vectors to implement List interfaces and maintain insertion order. However, there are several differences between the two. Below are four of the most prominent.
4 Differences between ArrayList and vector
Synchronisation: Vectors are synchronised, but ArrayList is not
Speed: Vectors are slower than ArrayLists because they are less synchronised, while ArrayLists are faster because they are non-synchronized
Iteration: Iteration in ArrayLists is achieved using the Iterator interface, whereas traversal in Lists is achieved by using the List interface.
Size: A vector increments 100% of its size when an element is added beyond its capacity, while an ArrayList increments 50% of its size if an element is added beyond its capacity.
Example of an ArrayList
import java.util.ArrayList; import java.util.Iterator; public class Arraylistszz { public static void main(String[] args) { ArrayList<String> ourArrayList = new ArrayList<String>(); ourArrayList.add("Java"); ourArrayList.add("Python"); ourArrayList.add("C++"); ourArrayList.add("PHP"); System.out.println("Our ArrayList Elements Include:"); Iterator myiterator = ourArrayList.iterator(); while (myiterator.hasNext()) System.out.println(myiterator.next()); } } |
Output:
Our ArrayList Elements Include: Java Python C++ PHP |
Example of a Vector
import java.util.Enumeration; import java.util.Vector; public class JavaSample { public static void main(String[] args) { Vector<String> vectali = new Vector<String>(); vectali.addElement("Python"); vectali.addElement("Java"); vectali.addElement("Kotlin"); System.out.println("\n We have added the following items in our vector:"); Enumeration enumeli = vectali.elements(); while (enumeli.hasMoreElements()) System.out.println(enumeli.nextElement()); } } |
Output
We have added the following items in our vector: Python Java Kotlin |
Article Contributed By :
|
|
|
|
96 Views |