Using ArrayList in Java
ArrayList is a dynamic array that implements the List interface. The user of this interface has full control over the inserted elements and accesses them by position as well as searching for the elements in the list.ArrayList implements all the methods of List, more than that, the class has its own methods like manipulating the size of the array used to store the list. This class is equivalent to Vector.
Arraylist uses an array that stores data, this array has a capability that automatically adapts each time an element is inserted. There is an ensureCapacity method that increases the capacity of the ArrayList before adding a large number of items to ensure the size.
The list is accessed simultaneously by multiple threads. This can cause problems when it comes to a modification, insertion, deletion because another thread is going to access and update the size of the list is in progress. The solution is to synchronize processes using the Collections.synchronizedList.
List list = Collections.synchronizedList(new ArrayList(...));
To browse the list with the iterator or listIterator class, but if the list has been modified: delete, insert... After creating the iterator, the latter will throw an exception ConcurrentModificationException. The solution is to create a mutual exclusion whose purpose is to prevent other threads from accessing it after iterator is created and during read.
Constructors
ArrayList has three constructors:- ArrayList(): Creates an empty list with an initial size of 10.
- ArrayList(Collection extends E> c): Creates a list from a data collection and throws an exception NullPointerException if the collection is null.
- ArrayList(int capacite): Creates a list by setting the initial size and throws an IllegalArgumentException if the size is negative.
Methods
1) add(Object o): adds an item to the end.list.add("Hello");
2) add(int indice, Object o): Insert in middle.
list.add(3, "e"); |
3) addAll(Collection c): Add a collection.
ArrayList l1 = new ArrayList(); l1.add("word"); l1.add(12); l1.add(10.4f); list.addAll(l1); |
4) addAll(int index, Collection c): Add a collection in the middle.
list.addAll(3, l1); |
5) clear(): Delete all elements.
6) contains(Object o): Element Membership.
boolean b = list.contains(o) |
8) ensureCapacity(int capacite): increases the capacity, it ensures that it can hold a number of elements with its minimum capacity.
list.ensureCapacity(10); |
9) get(int index): returns the object to one position.
system.out.println(list.get(2)); |
10) indexOf(Object o): Returns the index of an element.
int k = indexOf("o4"); |
11) isEmpty(): Returns true if the list is empty.
boolean b = list.isEmpty(); |
12) remove(Object o): Deletes the first occurrence of the o.
boolean b = list.remove("o4"); |
13) removeAll(Collection > c): Deletes elements that are in the collection passed as arguments.
AarrayList< String> lc = new ArrayList< String> (); lc . add("o1"); lc . add("o2"); lc . add("o3"); list.removeAll(lc); |
14) removeRange( int start, int end): Removes elements that are between the start and end subscripts.
list.removeRange(3,8); |
15) retainsAll(Collection > c): keeps only the elements that are in the collection c.
16) set(int index, Object o): modifies the object to a specific position.
list.set(3, "o5"); |
17) size(): returns the number of elements.
18) subList(int start, int end): returns the fragment between the beginning and the end.
19) toArray(): returns an array of one dimension.
String[] t = list.toArray(); |
20) trimToSize(): Reduces storage capacity to the maximum level.
How to browse an ArrayList
You can browse an ArrayList with two methods:1) Loop for
for(int i = 0; i < list.size(); i++) system.out.println(list.get(i)); or for example if we know the type: for(Integer number: list) system.out.println(number); |
2) Iterator + While
Iterator itr = list.iterator(); while(itr.hasNext()) system.out.println(itr.next()); |
Example
import java.util.ArrayList;References:
public class Test {
public static void main(String[] args) {
//create an arraylist with an initial capacity of 4
ArrayListstr = new ArrayList (4);
//Add
str.add("o1");
str.add("o2");
str.add("o3");
str.add("o4");
//Some methods we've seen
System.out.println("index of "+"o2: "+str.indexOf("o2"));
System.out.println("o3 exists?" +str.contains("o3"));
System.out.println("o2 successfully removed: "+str.remove("o2"));
System.out.println("size: "+str.size());
System.out.println("[1, 3] : "+str.subList(1, 3));
//parcours
for(String s : str)
System.out.println(s);
str.clear();
System.out.println("list is empty?" +str.isEmpty());
}
}
javadoc: ArrayList