Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Arraylist vs Vector

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 400
    Comment on it

    Basic difference between the two is the Synchronization. Vector is Synchronized and Arraylist is not. If your application will  be having single thread of execution use Arraylist else Vector.

    ArrayList is fast because its not synchronized. Its not a legacy class and was introduced in JDK 1.2. It uses Iterator interface to traverse the elements and once element exceeds the capacity it increment the size by 50% of current array size.

    Vector is slow due to synchronization. Its legacy class and use both Enumeration and Iterator to traverse the elements. And once the element exceed the capacity it increment the size by 100% of current size.

    Both implements the List interface and maintains the insertion order.

    Following is sample ArrayList Code:

          ArrayList arrayList= new ArrayList();
          // Add elements to the array list
          arrayList.add("CC");
          arrayList.add("AA");
          arrayList.add("DD");
          // Adding element at second position i.e. after CC
          al.add(1, "A2");
    
          // al.size() inform number of elements in ArrayList
          System.out.println("ArrayListSize :"+al.size());
    
          // Removing AA record from the ArrayList
          al.remove("AA");
          // Removing element at third position i.e. DD
          al.remove(2);
    
          // Get elements of ArrayList
          for(int i=0;i<al.size();i++)
          {
             System.out.println("ArrayList Element "+i+" :"+al.get(i));
          }

    Following is the sample Vector Code:

            Vector<String> vect=new Vector<String>();
    
            // Add elements to vector 
            vect.add("CC");
            vect.add("AA");
            vect.add("DD");
            
            // Add vector element at after CC
            vect.add(1, "A2");
     
            // vect.size() inform number of elements in Vector
            System.out.println("Vector Size :"+vect.size());
    
            // Removing AA record from the ArrayList
            vect.remove("AA");
            // Removing element at third position i.e. DD
            vect.remove(2);
    
            // Get elements of Vector 
            for(int i=0;i<vect.size();i++)
            {
                System.out.println("Vector Element "+i+" :"+vect.get(i));
            }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: