---
description: ArrayList in Java is a data structure that can be stretched to accommodate additional elements within itself and shrink back to a smaller size when elements are removed.
title: ArrayList in Java
image: https://www.guru99.com/images/uploads/2012/08/ArrayList.png
---

[Skip to content](#main) 

## What is ArrayList in Java?

**ArrayList in Java** is a data structure that can be stretched to accommodate additional elements within itself and shrink back to a smaller size when elements are removed. It is a very important data structure useful in handling the dynamic behavior of elements.

Wondering how ArrayList Java could be useful, see the below conversation –

[![Arraylist in Java](https://www.guru99.com/images/uploads/2012/08/ArrayList.png)](https://www.guru99.com/images/uploads/2012/08/ArrayList.png)

For Java ArrayList Example, see the following picture of a man stretching an elastic rubber band. The actual length of the rubber band is much smaller, but when stretched it can extend a lot more than its actual length and can be used to hold/bind much larger objects with it. Now, consider the next picture, that of a simple rope, it cannot stretch and will have a fixed length.

[](https://www.guru99.com/images/Array%5Flist.png)

It can grow as, and when required to accommodate the elements it needs to store and when elements are removed, it can shrink back to a smaller size. So as our friend has an issue with the array he is using cannot be expanded or made to shrink, we will be using ArrayList. [Arrays](https://www.guru99.com/java-arrays.html) are like the rope shown in the above picture; they will have a fixed length, cannot be expanded nor reduced from the original length. So our stretchable rubber-band is much like the Array List whereas the rope can be considered as the array. Technically speaking, ArrayList Java is like a dynamic array or a variable-length array.

Let us see and understand the following code snippet of Java ArrayList Syntax that will help you work around with ArrayList.

ArrayList<Object> a = new ArrayList<Object>();

## ArrayList Methods in Java

* **ArrayList add**: This is used to add elements to the Array List. If an ArrayList already contains elements, the new element gets added after the last element unless the index is specified.  
**Syntax:**  
add(Object o);
* **ArrayList remove**: The specified element is removed from the list and the size is reduced accordingly. Alternately, you can also specify the index of the element to be removed.  
**Syntax:**  
remove(Object o);
* **Java array size**: This will give you the number of elements in the Array List. Just like arrays, here too the first element starts with index 0.  
**Syntax:**  
int size();
* **ArrayList contains**: This method will return true if the list contains the specified element.  
**Syntax:**  
boolean contains(Object o);

## Java ArrayList Example

Following is a Java ArrayList Example:

import java.util.ArrayList;
class Test_ArrayList {
 public static void main(String[] args) {
  //Creating a generic ArrayList
  ArrayList<String> arlTest = new ArrayList<String>();
  //Size of arrayList
  System.out.println("Size of ArrayList at creation: " + arlTest.size());
  //Lets add some elements to it
  arlTest.add("D");
  arlTest.add("U");
  arlTest.add("K");
  arlTest.add("E");

  //Recheck the size after adding elements
  System.out.println("Size of ArrayList after adding elements: " + arlTest.size());

  //Display all contents of ArrayList
  System.out.println("List of all elements: " + arlTest);

  //Remove some elements from the list
  arlTest.remove("D");
  System.out.println("See contents after removing one element: " + arlTest);

  //Remove element by index
  arlTest.remove(2);
  System.out.println("See contents after removing element by index: " + arlTest);

  //Check size after removing elements
  System.out.println("Size of arrayList after removing elements: " + arlTest.size());
  System.out.println("List of all elements after removing elements: " + arlTest);

  //Check if the list contains "K"
  System.out.println(arlTest.contains("K"));

 }
}

**Output:** 

Size of ArrayList at creation: 0
Size of ArrayList after adding elements: 4
List of all elements: [D, U, K, E]
See contents after removing one element: [U, K, E]
See contents after removing element by index: [U, K]
Size of arrayList after removing elements: 2
List of all elements after removing elements: [U, K]
true

Note: For simplicity, the elements shown in above code are single character elements. We can add Strings, integers, etc.

[](https://www.guru99.com/images/uploads/2012/08/ArrayList4.png)

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
 Please check your inbox.

![AI-Newsletter]()Chosen by over **350,000+** professionals

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search 

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/uploads/2012/08/ArrayList.png","url":"https://www.guru99.com/images/uploads/2012/08/ArrayList.png","width":"200","height":"200","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/arraylist-in-java.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/arraylist-in-java.html","name":"ArrayList in Java"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/arraylist-in-java.html#webpage","url":"https://www.guru99.com/arraylist-in-java.html","name":"ArrayList in Java","dateModified":"2024-11-08T17:14:07+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/uploads/2012/08/ArrayList.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/arraylist-in-java.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"Article","headline":"ArrayList in Java","keywords":"java","dateModified":"2024-11-08T17:14:07+05:30","articleSection":"Java Tutorials","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"publisher":{"@id":"https://www.guru99.com/#organization"},"description":"ArrayList in Java is a data structure that can be stretched to accommodate additional elements within itself and shrink back to a smaller size when elements are removed.","name":"ArrayList in Java","@id":"https://www.guru99.com/arraylist-in-java.html#richSnippet","isPartOf":{"@id":"https://www.guru99.com/arraylist-in-java.html#webpage"},"image":{"@id":"https://www.guru99.com/images/uploads/2012/08/ArrayList.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/arraylist-in-java.html#webpage"}}]}
```
