Master ArrayList in Java
By: Tejas M R
Tags: Programming, Java, Arraylist, Documentation, Tutorial

Have you ever experienced these — “Wait! I remember learning Collections in Java, but I can’t remember the specifics!” or “Tomorrow is my Java interview and I know nothing about Collections classes!”. If so, you are at the right place. This is the first of a set of articles I am writing to help you get familiar with the Collection classes in java.

For most applications that are written in Java, developers prefer to use Collection types compared to the traditional array because of all the advantages they bring. Just the ability to have a dynamic number of elements is a boon for creating low-memory intensive applications. I want to help you learn all the Collection types — You can refer to this set of articles for your reference in the future too!

Introduction

We use the class ArrayList to create a dynamic array of objects of the same class. It is like a traditional array but there is no size limit — by extension, there is no IndexOutOfRangeException. Another difference is that we can’t access elements of an ArrayList using subscripts (like a[0]), we have to use a method in the ArrayList class to achieve that.

By the end of this tutorial, you will have a big java class with all the functions from which you can refer. Note that this is not an exhaustive guide, but this will cover all the common and not-so-common functions used with ArrayList class. Also, most of the tutorial is in the code and the code comments, so please read them if you are not familiar with the code.

Creating an ArrayList

Creating an ArrayList is very similar to creating any other object but we pass a type parameter since ArrayList is a generic class.

Add an element to an ArrayList

// Append an element

boolean add(E element); // Insert an element

void add(int index, E element);

Add a collection to an ArrayList

// Append a collection

boolean addAll(Collection<? extends E> collection); // Insert a collection

void addAll(int index, Collection<? extends E> collection);

Number of elements in an ArrayList

// Returns the number of elements in this list.

int size();

Remove an element from an ArrayList

// Removes the element at the specified position in this list and returns it.

E remove(int index); // Removes the first occurrence of the specified element from this list.

boolean remove(Object o);

Get an element at some index

// Returns the element at the specified position in this list.

E get(int index);

Modify/Set an element at some index

// Replaces the element at the specified position in this list with the specified element.

E set(int index, E element);

Other helpful functions

// Returns true if this list contains no elements.

boolean isEmpty(); // Removes all of the elements from this list.

void clear(); // Returns true if this list contains the specified element.

boolean contains(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 indexOf(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.

int lastIndexOf(Object o); // Returns a view of the portion of this list between the specified fromIndex , inclusive, and toIndex , exclusive.

List<E> subList(int fromIndex, int toIndex); // Returns a list iterator over the elements in this list (in proper sequence).

ListIterator<E> listIterator();

The easiest way of traversing an ArrayList

Thank you for reading! Be sure to follow me — Tejas M R, for more articles like this :)