ArrayList Class Java

Sharing is healthy !!

Hello everyone!! Welcome to flower brackets blog. Today we are going to learn arraylist class java.

introduction: arraylist class java

arraylist class java

So here is the hierarchy of arraylist class. Arraylist class extends abstractlist. Abstractlist implements list interface.

And list interface extends collection interface. Collection interface extends iterable interface.

Here whatever methods available in the list interface and collection interface will be supported by the arraylist class.


java.util

Class ArrayList<E>

java.lang.Object
     java.util.AbstractCollection<E>
          java.util.AbstractList<E>
               java.util.ArrayList<E>

Implemented interfaces

Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

Subclasses

AttributeList, RoleList, RoleUnresolvedList

The class arraylist belongs to is java.util package. As you can see above letter E is nothing but type.

It can be string, integer, employee object etc., and some of the implemented interfaces are as shown above.

Moving on some of the known subclasses, you can see above.


what is arraylist ?

Arraylist is an ordered collection. It contains group of objects, often referred to as elements.

arraylist class java

Above you can see an arraylist. There are four objects. In 0th index there is object “Obj 1”, in first index there is object “Obj 2” and so on.

Based on the index position we can access the object.

Arraylist implements all optional list operations and allows duplicate elements.

Also we can insert null elements and arraylist maintains insertion order.

Also Read – HashSet Java Collection Framework

Arraylist is not synchronised. Underlying data structure of arraylist is resizable array or growable array.

Arraylist is random access because arraylist works at the index basis.

Manipulation of arraylist is slow because there is lot of shifting.


example: ArrayList Class Java

import java.util.*;

public class ArrayListDemo
{
public static void main(String[] args)
{
// arraylist of string elements
ArrayList<String> al = new ArrayList<String>();
// adding elements to arraylist
al.add("orange");
al.add("mango");
al.add("strawberry");
al.add("cherry");
al.add("apple");
// printing arraylist elements
System.out.println("--------------ArrayList Class Java Example-------------------");
System.out.println("Displaying arraylist elements : " + al);

// now adding elements at given index
al.add(0,"Pear");
al.add(1,"Pineapple");

// now removing elements from the arraylist al.remove("strawberry");
al.remove("cherry");

System.out.println("After adding and removing elements : " + al);

// removing element from the given index
al.remove(2);

System.out.println("The final arraylist looks like this : " + al);
}
}

Output:

ArrayList Class Java

 


Methods supported by arraylist

Arraylist implements all the methods available in the list interface. So what all methods supported by the list will be supported by the arraylist.

Now let’s see the methods in list interface

MethodsDescription
boolean add ( E e )appends the specified element to the end of this list (optional operation).
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 (optional operation).
void add( int index, E element )inserts the specified element at the specified position in this list (optional operation).
boolean addAll( int index, Collection c )inserts all of the elements in the specified collection into this list at the specified position (optional operation).
E remove( int index )removes the element at the specified position in this list (optional operation).
boolean remove( Object o )removes the first occurrence of the specified element from this list, if it is present (optional operation).
boolean removeAll( Collection c )removes from this list all of its elements that are contained in the specified collection (optional operation).
boolean retainAll( Collection c )retains only the elements in this list that are contained the specified collection (optional operation).
E set( int index, E element )replaces the element at the specified position in this list with the specified element (optional operation).
boolean contains( Object o )returns true if this list contains the specified element.
boolean containsAll( Collection c )returns true if this list contains all of the elements of the specified collection.
E get( int index )returns the element at the specified position in this list
int indexOf( Object o )returns the index of the first occurrence of the specified element in this list, or - 1 if this list does not contain the element.
int lastIndexOf( Object o )returns the index of the last occurrence of the specified element in this list, or - 1 if this list does not contain the element.
void clear( )removes all of the elements from this list (optional operation).
boolean isEmpty( )returns true if this list contains no elements.
int size( )returns the number of elements in this list.
List subList( int fromIndex, int toIndex )returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Iterator iterator( )returns an iterator over the elements in this list in proper sequence.
ListIterator listIterator( )returns a list iterator over the elements in this list (in proper sequence).
ListIterator listIterator( int index )returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
Object[ ] toArray( )returns an array containing all of the elements in this list in proper sequence (from first to last element).
T[ ] toArray( T[] a )returns an array containing all of the elements in this list in proper sequence (from first to last element), the runtime type of the returned array is that of the specified array.

arraylist constructors

Interface

java.util.Collection<E>

Interface

java.util.List<E>

java.util.ArrayList<E>

♦ ArrayList()
♦ ArrayList(c:Collection<? extends E>)
♦ ArrayList(initialCapacity: int)
♦ trimToSize(): void

Arraylist belongs to java.util package. Arraylist implements list interface. So whatever methods available in list interface are implemented by arraylist.

Arraylist has one extra method trimToSize() and it has three constructors they are,

  • ArrayList() – empty constructor which will create empty list with default initial capacity.
  • ArrayList(c:Collection<? extends E>) – this one is parameterized constructor which accepts collection. It will create the arraylist from an existing collection.
  • ArrayList(initialCapacity: int) – this accepts “int” value. it creates empty lists with the specified initial capacity.

conclusion

So this is about arraylist class java. I hope you have enjoyed and understood this post.

You can subscribe to my blog flower brackets if you haven’t already.

Reference – Oracle help center

Sharing is healthy !!

Leave a Reply

Your email address will not be published.