- ArrayList course of teaching is sub course of teaching of collection interface which implements to List interface.
- ArrayList course of teaching provides resizable-array thus it tin grow automatically every bit per requirement which resolves fixed Array limitation where you lot convey to pre-define the size of array.
- It tin agree zippo elements too.
- It is non synchronized implementation thus if multiple threads accessing it concurrently in addition to whatsoever ane from them modifies the entry in addition to then it must live synchronized externally.
- Also it tin agree duplicate elements.
- boolean add(E e) : It volition append given chemical component division at the cease of this ArrayList.
- void add(int index, due east element) : It volition add together specified chemical component division at the given index inwards list.
- boolean addAll(Collection<? extends E> c) : It volition append all the elements of given collection at the cease of array list. Appending fellowship volition live every bit 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 component division 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 every bit per given minCapacity argument.
- E get(int index) : It volition decease chemical component division 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 jibe in addition to 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 terminal 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 chemical component division from array listing which is stored at specified index.
- boolean remove(Object o) : It volition take specified element(first occurrence) from array list.
- boolean removeAll(Collection<?> c) : It volition take all elements from listing which are specified inwards given collection.
- protected void removeRange(int fromIndex, int toIndex) : It volition take elements from listing starting from fromIndex to toIndex.
- boolean retainAll(Collection<?> c) : It volition retain exclusively those elements inwards array listing which are specified inwards given collection.
- E set(int index, due east element) : It volition supervene upon given chemical component division 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 every bit per it's electrical flow size.
Below given event volition demo you lot exercise of ArrayList class's unlike methods.
ArrayListExample
package JAVAExamples; import java.util.ArrayList; import java.util.Iterator; populace course of teaching ArrayListClassExample { populace 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 : "); piece (itr.hasNext()) { System.out.println(itr.next()); } //Add special at specified index. a.add(5, "nine"); System.out.println("Now ArrayList items are : "+a); //Get special from specified index. System.out.println("Item at index 6 is : "+a.get(6)); //Remove special from array list. a.remove("three"); System.out.println("Now ArrayList items are : "+a); //Get index of chemical component division from array list. System.out.println("Index of ane is : "+a.indexOf("one")); //Get terminal index of chemical component division from array list. System.out.println("Last Index of ane is : "+a.lastIndexOf("one")); } }
Output :
ArrayList items are : ane ii iii iv ane 6 7 8 Now ArrayList items are : [one, two, three, four, one, nine, six, seven, eight] Item at index 6 is : 6 Now ArrayList items are : [one, two, four, one, nine, six, seven, eight] Index of ane is : 0 Last Index of ane is : 3