- ArrayList course of report is sub course of report of collection interface which implements to List interface.
- ArrayList course of report provides resizable-array hence it tin grow automatically equally per requirement which resolves fixed Array limitation where y'all convey to pre-define the size of array.
- It tin concord null elements too.
- It is non synchronized implementation hence if multiple threads accessing it concurrently too whatever i from them modifies the entry hence it must live synchronized externally.
- Also it tin concord duplicate elements.
- boolean add(E e) : It volition append given chemical gene at the destination of this ArrayList.
- void add(int index, due east element) : It volition add together specified chemical gene at the given index inwards list.
- boolean addAll(Collection<? extends E> c) : It volition append all the elements of given collection at the destination of array list. Appending guild volition live equally per returned yesteryear collection's iterator.
- boolean addAll(int index, Collection<? extends E> c) : It volition insert all elements of specified collection inwards ArrayList starting from given index.
- void clear() : It volition clear List yesteryear removing all elements from it.
- Object clone() : It volition render shallow re-create of ArrayList.
- boolean contains(Object o) : It volition render truthful if given chemical gene is acquaint inwards list.
- void ensureCapacity(int minCapacity) : It volition increases the capacity of array listing if required to brand certain that it tin shop the elements equally per given minCapacity argument.
- E get(int index) : It volition become chemical gene from specified inced from array list.
- int indexOf(Object o) : It volition render the index of specified object(first occurrence) from array list.
- boolean isEmpty() : It volition banking concern gibe too render truthful if array listing is empty.
- Iterator<E> iterator() : It volition render an iterator over the elements from array list.
- int lastIndexOf(Object o) : It volition render index of concluding occurrence of specified element.
- ListIterator<E> listIterator() : It volition render the listing iterator over the elements of array list.
- ListIterator<E> listIterator(int index) : It volition render the listing iterator over the elements of array listing starting from specified index.
- E remove(int index) : It volition take away chemical gene from array listing which is stored at specified index.
- boolean remove(Object o) : It volition take away specified element(first occurrence) from array list.
- boolean removeAll(Collection<?> c) : It volition take away all elements from listing which are specified inwards given collection.
- protected void removeRange(int fromIndex, int toIndex) : It volition take away elements from listing starting from fromIndex to toIndex.
- boolean retainAll(Collection<?> c) : It volition retain entirely those elements inwards array listing which are specified inwards given collection.
- E set(int index, due east element) : It volition supervene upon given chemical gene inwards array listing at specified position.
- int size() : It volition render size of array list.
- List<E> subList(int fromIndex, int toIndex) : It volition render sub listing of elements from this listing starting from fromIndex(inclusive) to toIndex(exclusive).
- Object[] toArray() :It volition render proper sequential array of all array listing elements.
- void trimToSize() :It volition cut size of array listing equally per it's electrical current size.
Below given event volition demo y'all utilize of ArrayList class's dissimilar methods.
ArrayListExample
package JAVAExamples; import java.util.ArrayList; import java.util.Iterator; world course of report ArrayListClassExample { world static void main(String[] args) { // Create array list. ArrayList<String> a = novel ArrayList<String>(); // Add elements inwards array list. a.add("one"); a.add("two"); a.add("three"); a.add("four"); a.add("one"); a.add("six"); a.add("seven"); a.add("eight"); //Iterating over the arraylist elements. Iterator itr = a.iterator(); System.out.println("ArrayList items are : "); acre (itr.hasNext()) { System.out.println(itr.next()); } //Add detail at specified index. a.add(5, "nine"); System.out.println("Now ArrayList items are : "+a); //Get detail from specified index. System.out.println("Item at index 6 is : "+a.get(6)); //Remove detail from array list. a.remove("three"); System.out.println("Now ArrayList items are : "+a); //Get index of chemical gene from array list. System.out.println("Index of i is : "+a.indexOf("one")); //Get concluding index of chemical gene from array list. System.out.println("Last Index of i is : "+a.lastIndexOf("one")); } }
Output :
ArrayList items are : i 2 3 4 i vi vii 8 Now ArrayList items are : [one, two, three, four, one, nine, six, seven, eight] Item at index 6 is : vi Now ArrayList items are : [one, two, four, one, nine, six, seven, eight] Index of i is : 0 Last Index of i is : 3