Monday , December 10 2018
Home / Java / ArrayList in Java

ArrayList in Java

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

Example of Java ArrayList class

Apart from the methods inherited from its parent classes, ArrayList defines following methods:
1.  void add(int index, Object element)
Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index > size()).

2.  boolean add(Object o)
Appends the specified element to the end of this list

3.  boolean addAll(Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator. Throws NullPointerException if the specified collection is null.

4.  boolean addAll(int index, Collection c)
Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws NullPointerException if the specified collection is null
5. void clear()
Removes all of the elements from this list.

6 .Object clone()
Returns a shallow copy of this ArrayList.

7 .boolean contains(Object o)
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

8. void ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

9 . Object get(int index)
Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()).

10.  int indexOf(Object o)
Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.

11 . int lastIndexOf(Object o)
Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.

Two ways to iterate the elements of collection in java

  1. By Iterator interface.
  2. By for-each loop.

In the above example, we have seen traversing ArrayList by Iterator. Let’s see the example to traverse ArrayList elements using for-each loop.
Iterating the elements of Collection by for-each loop

User-defined class objects in Java ArrayList

 

About admin

Check Also

Internal Working of HashMap in Java

In this article, we will see how hashmap’s get and put method works internally. What ...

Leave a Reply