Questions tagged
arraylist
A simple collection data type found in some languages / platforms (such as in Java or .NET). The array list implements a list using an array, benefiting from both the DSs strengths.

ArrayList often denotes an abstract data type (ADT) found in some common programming languages / platform that represents a "growable" (dynamically-sized) array.

In Java:

java.util.ArrayList is a class in the Java SE standard library that implements the List interface. The ArrayList class represents the list contents using an Object array, which it reallocates as required to accommodate growth of the list.

The complexity of common List operations on arrays is as follows:

  • List.add(Object) - O(1) amortized (see below).
  • List.get(int) - O(1)
  • List.remove(int) - O(N) in the general case
  • List.contains(Object) - O(N) in the general case

The amortized cost of add is based on the following argument. Suppose that you start with an empty array list, and add N elements one at a time. Each time you add an element, the add method checks if the list's backing array is full. If it is, then it allocates a new backing array of twice the size as the current one, and then copies the existing elements from the old array to the new one. If you count up the number of element copies that occur in this process, you will find that when the array size expands to N == initial * 2^P, a total of initial *2^P - 1 copies will have taken place, and that is less than N copies. Then add N assignments of new elements, and bounds checks, and the total cost is clearly proportional to N, when accumulated over N calls to add. Then makes the average cost of a single call a constant; i.e. O(1).

In .NET:

System.Collections.ArrayList in the .NET Framework Class Library, like its counterpart in Java, also represents a dynamically-sized array. It has become mostly obsolete with the introduction of the generic System.Collections.Generic.List<T> type in .NET 2, which has the advantage (in most cases) of being strongly typed, and of avoiding unnecessary boxing of value types.

Related tags

arrays list