Disadvantages of ArrayList in C#

ArrayList, Vector, LinkedList difference and its advantages and disadvantages?

Mirza Adil

Nov 22, 2017·7 min read

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

Chủ Đề