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

32239 questions
3788
votes
41 answers

Create ArrayList from array

I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList arraylist = ???;
Ron Tuffin
  • 49,960
  • 23
  • 62
  • 76
3293
votes
33 answers

When to use LinkedList over ArrayList in Java?

I've always been one to simply use: List names = new ArrayList<>(); I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code. When should LinkedList be used over ArrayList and…
sdellysse
  • 36,011
  • 9
  • 23
  • 24
2940
votes
32 answers

Initialization of an ArrayList in one line

I wanted to create a list of options for testing purposes. At first, I did this: ArrayList places = new ArrayList(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); Then, I refactored the code as…
Macarse
  • 87,001
  • 42
  • 169
  • 229
1182
votes
6 answers

Convert ArrayList to String[] array

I'm working in the android environment and have tried the following code, but it doesn't seem to be working. String [] stockArr = (String[]) stock_list.toArray(); If I define as follows: String [] stockArr = {"hello", "world"}; it works. Is there…
locoboy
  • 34,978
  • 62
  • 174
  • 253
1125
votes
17 answers

Converting 'ArrayList to 'String[]' in Java

How might I convert an ArrayList object to a String[] array in Java?
Alex
  • 14,719
  • 6
  • 20
  • 19
662
votes
22 answers

How to get the last value of an ArrayList

How can I get the last value of an ArrayList? I don't know the last index of the ArrayList.
Jessy
  • 13,883
  • 31
  • 80
  • 100
582
votes
11 answers

Convert list to array in Java

How can I convert a List to an Array in Java? Check the code below: ArrayList tiendas; List tiendasList; tiendas = new ArrayList(); Resources res = this.getBaseContext().getResources(); XMLParser saxparser = new…
colymore
  • 9,604
  • 13
  • 45
  • 84
531
votes
38 answers

How do I remove repeated elements from ArrayList?

I have an ArrayList, and I want to remove repeated strings from it. How can I do this?
user25778
  • 5,475
  • 3
  • 18
  • 12
520
votes
17 answers

Why do I get an UnsupportedOperationException when trying to remove an element from a List?

I have this code: public static String SelectRandomFromTemplate(String template,int count) { String[] split = template.split("|"); List list=Arrays.asList(split); Random r = new Random(); while( list.size() > count ) { …
Pentium10
  • 190,605
  • 114
  • 394
  • 474
449
votes
12 answers

ArrayList vs List<> in C#

What is the difference between ArrayList and List<> in C#? Is it only that List<> has a type while ArrayList doesn't?
scatman
  • 13,171
  • 20
  • 66
  • 92
392
votes
21 answers

How to sort an ArrayList?

I have a List of doubles in java and I want to sort ArrayList in descending order. Input ArrayList is as below: List testList = new…
Himanshu
  • 4,103
  • 2
  • 13
  • 16
373
votes
7 answers

What are the differences between ArrayList and Vector?

What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?
KushalP
  • 10,172
  • 6
  • 30
  • 27
371
votes
10 answers

How to avoid "ConcurrentModificationException" while removing elements from `ArrayList` while iterating it?

I'm trying to remove some elements from an ArrayList while iterating it like this: for (String str : myArrayList) { if (someCondition) { myArrayList.remove(str); } } Of course, I get a ConcurrentModificationException when trying to…
Ernestas Gruodis
  • 7,563
  • 12
  • 45
  • 104
356
votes
8 answers

How to quickly and conveniently create a one element arraylist

Is there a Utility method somewhere that can do this in 1 line? I can't find it anywhere in Collections, or List. public List stringToOneElementList(String s) { List list = new ArrayList(); list.add(s); return…
David T.
  • 18,561
  • 18
  • 61
  • 115
353
votes
27 answers

Best way to convert an ArrayList to a string

I have an ArrayList that I want to output completely as a String. Essentially I want to output it in order using the toString of each element separated by tabs. Is there any fast way to do this? You could loop through it (or remove each element) and…
Juan Besa
  • 4,381
  • 5
  • 22
  • 25
1
2 3
99 100