Java ArrayList: A Complete Guide for Beginners

Last updated on Jun 05,2023 11.2K Views
A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.

Java ArrayList: A Complete Guide for Beginners

edureka.co

We all know that arrays are an important structure in Java which can be used to store static data. But, what if your data needs to be stored dynamically? In order to do this, Java offers a special collection framework called Java ArrayList that is specifically meant to store dynamic data. In this article, I will introduce you to this advanced class of arrays and show you how they differ from their static counterparts. Here are the topics that will be covered in this tutorial:

Introduction to ArrayList

ArrayList is the implementation of List Interface where the elements can be dynamically added or removed from the list. Also, the size of the list is increased dynamically if the elements are added more than the initial size. Though it may be slower than standard arrays, it can be helpful in programs where lots of manipulation in the array is needed.

So how do you declare an ArrayList?

It’s very simple. Declaring an ArrayList is as follows.
Declaration:  ArrayList al= new ArrayList();
al ->is a reference to an ArrayList that holds references to objects.

 

What is the use of ArrayList?

Java ArrayList Tutorial | Java ArrayList Examples | Java Tutorial For Beginners | Edureka

This video on will give you a brief insight about ArrayList in Java and its various constructors and methods along with an example.


Hierarchy of ArrayList in the Collections Framework

ArrayList uses a dynamic array for storing the elements. It inherits AbstractList class and implements List interface. The List interface extends Collection and Iterable interfaces in hierarchical order.

Subscribe to our YouTube channel to get new updates..!

Constructors in ArrayList

ConstructorDescription
  1. ArrayList()         Used to build an empty array list
  2. ArrayList(Collection c)         Builds an array list that is initialized with the elements of collection c
  3. ArrayList(int capacity)         Used to build an array list that has the specified initial capacity

Example Illustrating the use of Constructors:

public class Constructors {
public static void main(String args[]){ 
ArrayList a = new ArrayList(); // Creating a new ArrayList

  int counter = 0;
  for(String: a ) {
    counter++;
 }
System.out.println("No arguments: (can't obtain)" + counter);
ArrayList b = new ArrayList(41); // Initializing capacity to ArrayList
counter = 0;
for(String: b ) {
 counter++;
}

System.out.println("Argument with capacity: (can't obtain)" + counter);
System.out.println();
String sArray[] = {"Bangalore", "Delhi", "Mumbai", "Pune", "Kerala", "Gujurat"};
List list = Arrays.asList(sArray);
ArrayList c = new ArrayList(list);
c.add("Dharwad");
for(String s: c) {
System.out.println("ArrayList c element:" + s);
}
System.out.println(c);
}
}

 

    
Get Certified With Industry Level Projects & Fast Track Your Career

Dive into the world of full stack development and unleash your creativity with our Full Stack Developer Course.

Java ArrayList Methods

package Edureka; 
import java.util.*; 
public class Arraylist { 
public static void main(String args[]) 
{ 
ArrayList al= new ArrayList();
al.add("Edureka); 
al.add("Java"); // Adding elements to ArrayList
al.add("Arrays");
 System.out.println("Size of arraylist:" +al.size());
 System.out.println("Contents of al:" +al); 
} 
}

In the above code, It adds the elements to the created ArrayList.

public class Arraylist {
public static void main(String args[])
{
ArrayList al= new ArrayList();
al.add("Edureka");
al.add("Java");
al.add("Arrays");
System.out.println("Size of arraylist:" +al.size());
System.out.println("Contents of al:" +al);
al.remove("Java"); // Removes Java element from the list
System.out.println("Contents of al:" +al);
}
}

Difference between ArrayList and Arrays:

ArrayListArrays
  It is of variable-length because it is dynamic in size    It is of fixed length.
  Can add different object and data into the list    Supports only primitive data-type
  Allows addition of duplicate elements    Does not support a duplicate addition
  Can traverse in both forward and backward direction    Can traverse only in a forward direction
  Size can be modified dynamically    Size cannot be modified dynamically

 

This brings us to the end of our blog on Java ArrayList. I hope this blog was informative and added value to your knowledge in Java.

Wish to learn Java from Scratch from Professionals?

Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Elevate your web development skills with our industry-relevant Node JS Course and stay ahead in the ever-evolving tech world.

Got a question for us? Please mention it in the comments section of this “Java ArrayList” blog and we will get back to you as soon as possible.

 

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES