A detailed explanation of the difference between vectors and ArrayList in Java _java

Source: Internet
Author: User

First of all, the list interface is implemented by both classes, and the list interface has three implementation classes, namely ArrayList, Vector, and LinkedList. The list is used to hold multiple elements, to maintain the order of elements, and to allow for duplication of elements.

The relevant differences between the 3 specific implementation classes are as follows:

1.ArrayList is the most commonly used list implementation class, which is implemented internally through arrays, allowing for fast random access to elements. The disadvantage of an array is that there can be no spacing between each element, and when the array size is not satisfied, the storage capacity needs to be increased, so that the data already in the array is copied into the new storage space. When inserting or deleting elements from the middle of the ArrayList, it is necessary to copy, move, and cost the array. Therefore, it is suitable for random lookup and traversal, unsuitable for inserting and deleting.

2.Vector, like ArrayList , is also implemented through arrays, except that it supports the synchronization of threads, that is, only one thread at a time can write vectors, avoiding inconsistencies caused by multiple threads writing simultaneously, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList.

3.LinkedList is used to store data in a linked list structure, which is suitable for dynamic insertion and deletion of data, random access and slow traversal speed. In addition, he provides methods that are not defined in the list interface, specifically for manipulating headers and footer elements, and can be used as stacks, queues, and bidirectional queues.

Look at the Java source and find that when the array is not large enough, the array needs to be ArrayList, and then the elements are copied into the new array, and the size of the extended array of vectors is different.

In ArrayList:

Public boolean Add (e) {
 ensurecapacity (size + 1);//add element to determine whether it can be accommodated. If you can't, you'll create a new array 
 elementdata[size++] = e;
 return true;
}
 public void ensurecapacity (int mincapacity) {
 modcount++; 
 int oldcapacity = elementdata.length;
 if (Mincapacity > Oldcapacity) {
  Object olddata[] = elementdata;//This row is not useful, I don't know what the developer is thinking about 
  int newcapacity = (o Ldcapacity * 3)/2 + 1; Add the size of the new array 
  if (Newcapacity < mincapacity)
  newcapacity = mincapacity;
   Mincapacity is usually close to size, and so is a win: 
   Elementdata = arrays.copyof (Elementdata, newcapacity); 
   }

}

Vector Medium:

private void Ensurecapacityhelper (int mincapacity) {
 int oldcapacity = elementdata.length;
 if (Mincapacity > Oldcapacity) {
  object[] olddata = elementdata;
  int newcapacity = (capacityincrement > 0)?
  (Oldcapacity + capacityincrement): (oldcapacity * 2);
  if (Newcapacity < mincapacity) {
  newcapacity = mincapacity;
  }
   Elementdata = arrays.copyof (Elementdata, newcapacity);
 }


The difference between ArrayList and vectors is as follows:

ArrayList is extended 50% + 1 By default when memory is not sufficient, and vector is 1 time times the default extension.
Vector provides indexof (obj, start) interface, ArrayList not.
The vector is thread-safe, but in most cases vector is not used because thread security requires a greater system overhead.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.