Array and ArrayList, List and LinkedList difference?

The following lists the different points Array (array) and ArrayList (sets):

  1. Array can contain primitive types and object types, ArrayList only contains the object type.

  2. Array size is fixed, the size of the ArrayList is dynamic.

  3. ArrayList methods and provides more features, such as: addAll (), removeAll (), iterator () and the like.

  4. For basic types of data, using an automatic packing set to reduce the coding effort. However, when dealing with the basic data types of fixed size, in this way is relatively slow.

First, the array (Array)

Java most basic data structure, a method of dynamically create and access java array, the Array type definition, which must be the same type of elements. Array is a fixed-size container is used in the bottom space to store continuous linear element.

Advantages: in the memory is continuous, fast, easy to operate.

Disadvantages: To define the length of the array definition, is not very flexible, too long, too short can cause problems. Add convenient, insertion and removal of data.

Second, the collection ArrayList

ArrayList is also a container, but its size is not fixed, the bottom is also used to store spatial continuous linear element when the linear element continuous space is insufficient to store, and re-apply a larger space (about 2 times the original space) will be moved over the original content .

So you can see from here, high efficiency Array than ArrayList, because the need to re-apply for space .

Advantages: a part of a namespace System.Collections. Size is dynamic expansion and contraction. It is not necessary to specify the length ArrayList object declaration. ArrayList inherited the IList interface, you can easily add, remove and insert the data.

Disadvantages: 当向集合插入不同类型的数据后(ArrayList将数据当作object存储)error type mismatch easily occurs during data processing, the type of conversion process required when used 存在装箱与拆箱操作,造成性能大量损耗的现象.

ArrayList list = new ArrayList();
list.Add("aa");
list.Add(11);
list[1] = 123;//修改
list.Remove(123);//移除
list.RemoveAt(0);

Third, the generic List

List class is a generic class ArrayList equivalence class (List interface inherits ArrayList). Most of its usage is similar to the ArrayList, because the List class (abstract class can not be instantiated directly) also inherited the List interface. The key difference is that when you declare a List collection, we at the same time 需要为其声明List集合内数据的对象类型.

Pros: Similar to the ArrayList collection and inherit IList interface, also declared a collection of object types when internal data collection in a statement List, avoids the problem of boxing and unboxing.

List<Integer> list=new ArrayList<Integer>();

list.Add(123);

list[0]=111;//修改

list.RemoveAt(0);//移除

If list.Add ( "abc"); // compiler will report an error, to avoid the type of performance problems and security issues of boxing and unboxing.

Here to explain the difference in the ArrayList and LinkedList

ArrayList and LinkedList implement the List interface, they have the following differences:

1.ArrayList data interface is based on an index, it is the underlying array. It can be a random access to the elements (1) time complexity O. Correspondingly, LinkedList is a list of elements stored in the form of its data, each element and a previous element and link it back together, in this case, seek time an element of complexity is O ( n).

2. respect ArrayList, LinkedList of insertion, addition, deletion faster, because when an arbitrary element is added to the set position, the size does not need to be recalculated as an index image, or update the array.

3.LinkedList accounted for more memory than the ArrayList, LinkedList because for each node stores the two reference points to a previous element, an element directed downward.

Guess you like

Origin blog.csdn.net/qunqunstyle99/article/details/95045693