Arrays in Java have a fixed size that is defined when they are created. This can be limiting when the size of the data that needs to be stored is not known beforehand or can change during runtime. To overcome this limitation, Java provides dynamic arrays through the ArrayList class.

An ArrayList is a resizable array implementation that allows elements to be added and removed dynamically. It automatically handles resizing the backing array as needed when elements are added. This makes ArrayLists very useful for storing data whose size is not known upfront or tends to change over time.

In this comprehensive guide, we will cover everything you need to know about creating dynamic arrays using ArrayList in Java, including:

  • What is ArrayList in Java
  • How to create an ArrayList
  • ArrayList constructors
  • Adding elements to an ArrayList
  • Accessing elements from an ArrayList
  • Removing elements from an ArrayList
  • Other useful ArrayList methods
  • Internal working of ArrayList
  • When to use ArrayLists vs arrays
  • ArrayList vs Vector

So let‘s get started!

What is ArrayList in Java

An ArrayList in Java is a dynamic array implementation built on top of an array data structure. It provides useful methods to add, remove, find elements in the list and iterate over them.

Here are some key points about ArrayList:

  • It implements the List interface and provides a powerful alternative to arrays.
  • The size of an ArrayList grows dynamically as elements are added. No need to pre-allocate memory like arrays.
  • Allows accessing elements by index (like arrays) but also supports adding/removing elements easily.
  • Maintains insertion order of elements, allowing duplicates.
  • Backed by an array internally and manages resizing array automatically.
  • Provides a lot of useful utility methods apart from basic add/remove/get operations.
  • Thread-safe variant provided through Vector class.

In short, ArrayList gives flexibility of dynamic sizing along with fast random access offered by arrays. This combination makes it very useful in Java programming.

How to Create an ArrayList in Java

There are a couple of ways to initialize an ArrayList in Java:

1. ArrayList Constructor

You can create an ArrayList by calling one of its constructors:

ArrayList list = new ArrayList(); //default initial capacity

ArrayList list = new ArrayList(int capacity); //specific capacity
  • The no-args constructor creates an empty ArrayList with default initial capacity.
  • The int-arg constructor allows specifying initial capacity of list.

2. ArrayList with Values

Initialize ArrayList directly with some elements:

ArrayList languages = new ArrayList(Arrays.asList("Java", "Python", "JavaScript"));
  • Pass an Collection (e.g. Arrays.asList) to ArrayList constructor to add initial elements.

3. Using List Interface

Define the type of objects list will hold:

List<String> names = new ArrayList<>(); //String ArrayList 
  • Use generics (List<E>) to specify type of elements for type safety.

That‘s the basics of creating an ArrayList instance in Java. Now let‘s go through the constructors and methods in more detail.

ArrayList Constructors in Java

There are 3 main ArrayList constructors:

1. ArrayList()

The no-arg constructor creates an empty ArrayList:

ArrayList list = new ArrayList();
  • Initializes backing array of size 10. Array size increases if more elements are added.

2. ArrayList(int initialCapacity)

Initialize ArrayList with specific capacity:

ArrayList list = new ArrayList(25);
  • Initialize backing array of given capacity to avoid resizing when elements are added.

3. ArrayList(Collection c)

Create ArrayList initialized with elements of another collection:

ArrayList list = new ArrayList(Arrays.asList("5", "7", "9"));
  • Backing array size equals to number of elements added.

The constructors make it easy to create ArrayLists in different scenarios like empty lists, with initial capacity or based on another collection.

Adding Elements to ArrayList

Elements can be added easily to an ArrayList using the add() method.

1. add(E element)

Add element to end of ArrayList:

ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple"); 
fruits.add("Mango");

2. add(int index, E element)

Insert element at given index, shifting existing elements:

fruits.add(1, "Banana"); //Apple, Banana, Mango

Some points about ArrayList add():

  • Inserts element in next available index if no index specified.
  • Can insert elements in middle by specifying index.
  • Expands backing array if needed while adding elements.

add() method makes it really easy to build up an ArrayList dynamically.

Accessing Elements in ArrayList

Elements can be read by specifying index, just like arrays:

String fruit = fruits.get(0); //returns Apple

Remember ArrayList index starts from 0.

Trying to access invalid index throws IndexOutOfBoundsException.

Useful Methods

Some other useful methods for accessing elements:

  • get(int index): Return element at specified index
  • size(): Get current size of ArrayList.
  • isEmpty(): Check if list contains no elements.

So ArrayList provides array-like index based access to its elements.

Removing Elements from ArrayList

As the contents within an ArrayList are dynamic, elements can be removed using:

1. remove(int index)

Remove element present at given index:

fruits.remove(1); //Removes Banana
  • After remove, elements are shifted to fill the gap at index.

2. remove(Object element)

Remove first occurrence of given element:

fruits.remove("Mango");

Some pointers on ArrayList remove():

  • Shifts elements to fill gap created after deletion.
  • Throws IndexOutOfBoundsException if invalid index passed.
  • Use iterator if removing more than one matching element.

Along with add(), the remove() API provides flexibility to modify ArrayList dynamically.

Other Useful ArrayList Methods

In addition to adding and removing elements, ArrayList contains many utility APIs:

1. Contains

Check if element exists in list:

boolean hasApple = fruits.contains("Apple");

2. Size and isEmpty

Get size and check emptiness:

int size = fruits.size();
boolean empty = fruits.isEmpty();

3. Clear

Remove all elements from ArrayList:

fruits.clear();

4. Sort

Sort elements in ascending order:

Collections.sort(fruits);  

5. Convert to Array

Convert to array for interoperability:

String[] array = fruits.toArray(new String[0]);

So ArrayList offers a lot of helpful methods to manipulate list contents, besides just storing elements.

How ArrayList Works Internally

  • ArrayLists are built by wrapping a dynamic array internally.

  • The size of backing array is increased automatically as needed while adding elements.

  • A new larger array is created and elements copied into it, if add makes array full.

This animation demonstrates ArrayList expansion:

ArrayList Internal Working Animation

Animation by Real Python

Key things about internal working:

  • ArrayList manages the backing array automatically.
  • Copies elements to new array as needed for expansion.
  • The client only sees a dynamically resizable array.

This frees you from handling fixed sized arrays and resizing operations.

When to Use ArrayLists vs Arrays?

Since ArrayList builds on arrays internally, when should you use one vs the other?

Use Arrays If:

  • Need speed and memory efficiency. Arrays use less memory and offer faster access.
  • Size of data is fixed or known upfront. Removing elements is difficult.

Use ArrayLists If:

  • Need dynamic sizing as size not known beforehand.
  • Require flexibility to grow, shrink on demand.
  • Inserting and deleting elements besides storage needed.

So go with ArrayLists when dynamic sizing is needed, arrays when size is fixed.

Difference between ArrayList and Vector

Both ArrayList and Vector provide dynamic arrays. But they have a key difference:

1. Synchronization

  • Vector is synchronized and thread-safe.
  • ArrayList is non-synchronized, can‘t use safely across threads.

2. Performance

  • Vector ops slower due to synchronization overhead.
  • ArrayList faster as non-synchronized.

So in single-threaded apps use ArrayList for best performance. Vector helpful in multi-threaded context where synchronization required.

Conclusion

ArrayLists provide a flexible, feature-rich dynamic array implementation that grows automatically as elements are added. This frees you from the hassle of managing fixed sized arrays.

We learned how to create, initialize ArrayList instances in different ways along with useful constructor options. Adding and retrieving elements is easy via index based access. Resizing array to accommodate additions and deletions happens seamlessly through helpful APIs.

ArrayLists form an integral part of Java collections and are useful in most applications needing some kind of growable indices based data storage. Implementing a dynamic array manually is an intricate task which ArrayList neatly wraps for you out of the box.

I hope you enjoyed this comprehensive overview of how to create and use dynamic arrays through ArrayList class in Java. Let me know if you have any other ArrayList tips or questions!

Similar Posts