The method “readIntegers” shows how to create an ArrayList which will contain integers. ArrayList is a generic class, which means you can specify what kinds of things will be stored in it using less than(<), greater than(>). ArrayLists can’t store basic types, only classes, but each basic type has a corresponding class. For “int”, that is the class “Interger”. You’ll note Interger is capitalized, because it is the name of a class. Java automatically converts between “int” and “Interger” as required. As with all objects, we must call a constructor to create one. Then, while there are still more ints to be read from the scanner, we call the add method on “result”, our ArrayList object, to add them. At the end, we return result.
The method “reverse” demonstrates how to use “get” and “set”. This method loops through the first half of the array, getting one item from the first half and its corresponding item from the second half and swapping them. We set i to 0, then move the 0th entry to temp, move the right side to 0 and move temp to the right side. Then i becomes 1. We move the 1st entry to temp, move the right side to 1 and move temp to the right side. Then i becomes 2. We move the 2nd entry to temp, move the right side to 2 and move the temp to the right side. Now the loop is over and the entries are all reversed.
The methods “print” and “alternatePrint” show two different ways of priniting all of the elements in the ArrayList. Print use a regular for loop that counts from 0 to size-1, and then does a get on the ith element of the ArrayList. “alternatePrint”, however, uses a special kind of Java for loop, called an “iterator”. Here “Interger value colon list” means there will be one iteration for each item in the ArrayList, and value will, in turn, be the 0th, 1st, 2nd, though size minus 1st numbered item in the ArrayList. Both of the loops do exactly the same thing.
Click on the image to make it larger.
public class ArrayList
implements List, RandomAccess, Cloneable, Serializable
All the operations (get, set, size, isEmpty, iterator, listIterator) run in constant time excepting the "add" operation which runs in amortized constant time. To add n elements it takes O(n) time wheras other operatoins run in linear time.
Each and every instance of ArrayList has a capacity which denotes the size of the array used to store the elements in list. When an element is added in ArrayList it's capacity increases automatically. The addition of an element has constant amortized time cost.
The "ensureCapacity" operation can be used to increase the capacity of an ArrayList instance.
Consider a situation where multiple threads are accessing the same ArrayList instance concurrently and one of the thread is modifing that list, it must be synchronized externally.
Note : The modification must be structural i.e. addition/deletion of elements, explicitly resizing the backiarray.
Remember, setting the value of an element is not considered as a structural modification.
umm so back to the point - we saw that we have to synchronize externally and that is accomplished by synchronizing on some object that encapsulates the list. If no such object exists then "wrap" the list using the method named as "Collections.synchronizedList" and to prevent accidental unsynchronized access to list, this wrapping is done at creation time.
List list = Collections.synchronizedList (new ArrayList (...) ) ;
The iterators returned by "iterator" and "listIterator" methods are fail-fast. If list is modified (structurally) at any time after the iterator is created the "iterator" will throw a ConcurrentModificationException. (excepting iterator's own remove or add methods)
PS: fail-fast behavior of an iterator can't be guaranteed as it is. It throw ConcurrentModificationException on a best-effort basis, there are no guarantees in the presence of unsynchronized concurrent modification. Hence it would be wrong to write a program that depends on this exception for its correctness. Remember, the fail-fast behavior should be used only to detect bugs.
Edited by Rahul :
"Program to interfaces, not implementations" means try to use interface references wherever possible.
and also returntype should be List < Integer >