Disadvantages of ArrayList in C#

Disadvantages of ArrayList in C#

ArrayList, LinkedList, Vestor these three classes have implemented java.util.List interface, but they have their own different characteristics, mainly as follows:

First, the synchronization

ArrayList, LinkedList is not synchronized, and Vestor is synchronized. So if you do not require thread safety, you can use ArrayList or LinkedList, you can save the overhead for synchronization. But in the case of multi-threaded, sometimes you have to use Vector. Of course, there are ways to wrap the ArrayList, LinkedList, so that they also achieve synchronization, but the efficiency may be reduced.

Second, the growth data

from the internal implementation mechanism in terms of ArrayList and Vector are using an array of Objec to store. When you add elements to both types, they all need to extend the length of the internal array if the number of elements exceeds the current length of the internal array. By default, Vector automatically grows by twice the size of the original ArrayList. Of the 50%, so in the end you get this collection always occupy more space than you really need. So if you want to save large amounts of data in a collection, there are some advantages to using Vector since you can avoid unnecessary resource overhead by setting the initial size of the collection.

Third, the efficiency of the search, insert, delete objects

ArrayList and Vector, from the specified location (index) to retrieve an object, or insert at the end of the collection, delete an object is the same time, can be expressed as O (1) . However, if you add or remove an element elsewhere in the collection, the time it takes increases linearly by: O (ni), where n is the number of elements in the collection and i is the indexs position where elements are added or removed. Why is this so? It is assumed that all the elements after the ith and ith elements in the set are to perform (ni) displacement operations on the objects when the above operation is performed.
In LinkedList, inserting and deleting elements anywhere in the collection takes the same amount of time, -O (1), but it is slower for indexing an element, O (i), where i is the indexs position .

Generally we all know ArrayList and LinkedList roughly the difference :

1.ArrayList is based on a dynamic array of data structures, LinkedList data structure based on the list.
For random access get and set, ArrayList think better than LinkedList because LinkedList to move the pointer.
For new and delete operations add and remove, LinedList is more advantageous because ArrayList wants to move the data.

ArrayList and LinkedList are two collection classes for storing a series of object references. For example, we can use ArrayList to store a series of String or Integer. So ArrayList and LinkedList in performance What is the difference? When should ArrayList use LinkedList?
one. Time Complexity
The first point is critical, internal ArrayList implementation is based on the foundation of an array of objects, therefore, it uses the get method to access any of a list of the element (random access), its speed is faster than LinkedList. The get method in LinkedList starts from the end of the list in order, checking until the other end. For LinkedList, there is no faster way to access a specific element in the list.
Suppose we have a very large list, the elements inside it have been sorted, the list may be ArrayList type may also be LinkedList type, and now we list the binary search (binary search), compare the list is ArrayList and LinkedList query speed, see the following program:

Java code
package com.mangocity.test;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class TestList {
public static final int N = 50000;

public static List values;

static {
Integer vals [] = new Integer [N];

For r

(int i = 0, currval = 0; i vals = new Integer (currval);
currval + = r.nextInt (100) +1;
}

values ​​= Arrays.asList (vals);
}

static long timeList (List lst) {
long start = System.currentTimeMillis
for (int i = 0; i int index = Collections.binarySearch (lst, values.get (i));
if (index! = i)
System.out.println (** * error *** );
}
return System.currentTimeMillis () Start;
}
public static void main (String args []) {
System.out.println ( the ArrayList elapsed time: + timeList (new ArrayList (values)));
System.out.println (LinkedList time-consuming: + timeList (new LinkedList (values)));
}
}

The output I get is: ArrayList Time-consuming: 15
LinkedList Time-consuming: 2596
This result is not fixed, but basically The time of the ArrayList is significantly less than the time of the LinkedList. Therefore, in this case should not use LinkedList. The binary search method uses a random access strategy, whereas LinkedList does not support fast random access. The time it takes to randomize a LinkedList is proportional to the size of the list. And accordingly, the time spent in random access in the ArrayList is fixed.
Does this indicate that ArrayList always performs better than LinkedList? This is not necessarily the case, LinkedList performs better than ArrayList in some cases, and some algorithms are more efficient when implemented in LinkedList. For example, using the Collections.reverse method to reverse the list, the performance will be better.
Look at such an example, join us to have a list, to its large number of insert and delete operations, in this case LinkedList is a better choice. Consider the following extreme case where we repeat the insertion of an element at the beginning of a list:

Java code
package com.mangocity.test;

import java.util. *;
Public class ListDemo {
static final int N = 50000;
static long timeList (List list) {
long start = System.currentTimeMillis ();
Object o = new Object ();
for (int i = 0; i list.add (0 , O);
return System.currentTimeMillis () Start;
}
public static void main (String [] args) {
System.out.println ( the ArrayList Processed: + timelist (the ArrayList new new ()));
the System.out .println (LinkedList time: + timeList (new LinkedList ()));
}
}
At this point my output is: ArrayList Time: 2463

LinkedList Time: 15
This is in contrast to the result of the previous example where when an element is added to the very beginning of an ArrayList, all existing elements are shifted backward, which means the overhead of data movement and copying. Conversely, adding an element to the beginning of the LinkedList is simply a matter of assigning a record to this element and then adjusting the two connections. The overhead of adding an element to the beginning of a LinkedList is fixed, while the cost of adding an element to the beginning of an ArrayList is proportional to the size of the ArrayList.

two. Space complexity
has LinkedList in a private internal class, defined as follows:

the Java Code
Private Entry {static class
Object Element;
Entry Next;
Entry Previous;
}

a reference list element for each object in the Entry, as well as in the LinkedList Its the previous element and the next element. A LinkedList object of 1000 elements will have 1000 linked Entry objects, each corresponding to an element in the list. In this case, there is a large overhead in a LinkedList structure because it stores information about the 1000 Entity objects.
ArrayList uses a built-in array to store elements, the starting capacity of this array is 10. When the array needs to grow, the new capacity is obtained as follows: new capacity = (old capacity * 3) / 2 +1, that is Each capacity will probably increase by 50%. This means that if you have an ArrayList object that contains a large number of elements, you will eventually have a lot of space to be wasted, which is caused by the way ArrayList works. If there is not enough space to hold the new element, the array will have to be re-allocated to be able to add new elements. Re-allocation of the array, will result in a sharp decline in performance. If we know how many elements an ArrayList will have, we can specify the capacity by the constructor. We can also remove the wasted space after the ArrayList has been allocated using the trimToSize method.

three. Summary
ArrayList and LinkedList have their own advantages and disadvantages in performance, have their own place, in general can be described as follows:
1. For ArrayList and LinkedList, the cost of adding an element to the end of the list is fixed. For ArrayList, it is mainly the addition of an item to the internal array that points to the added element, which occasionally causes the array to be reallocated. For LinkedList, this overhead is uniform and an internal Entry object is allocated.

2. Inserting or deleting an element in the middle of an ArrayList means that the remaining elements in the list are all moved; the overhead of inserting or deleting an element in the middle of a LinkedList is fixed.

3. LinkedList does not support efficient random element access.

4. ArrayList space waste is mainly reflected in the end of the list to reserve a certain amount of space, and LinkedList space cost is reflected in each of its elements need to consume considerable space

It can be said that using an ArrayList will provide better performance when the operation is to add data to the back of a column of data rather than in front of or in the middle of the column, and you need to randomly access the elements. When your operation is in front of a column of data Or add or delete data in the middle, and in order to access the elements, you should use LinkedList.

So, if you just look for elements in a particular location or just add to the end of the collection, remove the elements, then use Vector or ArrayList. If it is inserted into the other designated location, delete operation, the best choice LinkedList