Array List in Java: All You Need to Know About it

  • Written By  

  • Published on February 6th, 2023

Table of Contents [show]

 

Introduction

 

ArrayList is an element of the Java collection framework and it is a class of java.util package. It gives us dynamic arrays in Java. Though, it may be more gradual than standard arrays but can be useful in programs where the excess of manipulation in the array is required. This class is found in java.util package. The main benefits of ArrayList are that if we display an array, then it is required to mention the size, but in ArrayList, it is not required to mention the size of ArrayList if you want to.

 

 

Java ArrayList

 

The Java ArrayList class keeps the elements in a dynamic array. It is like an array with no size limit. We can add or remove elements anytime. So it is much more adaptable than the traditional array. The ArrayList in Java can have duplicate features as well. It executes the List interface, so we can use all the techniques of the List interface here. The array list supports the insertion order internally. It inherits the AbstractList class and executes the List interface.

 

The ArrayList class is a resizable array and can be found in java.util package. The contrast between a built-in array and an ArrayList in Java is that the size of an array cannot be changed (if you want to add or remove elements to or from an array, you have to form a new one). while elements can be added to and removed from an ArrayList whenever you want.

Our Learners Also Read: What is Overriding in Java?

 

 

Initialize ArrayList java

 

Here is a program to initialize ArrayList in Java using add() method

 

// Java code to illustrate initialization

// of ArrayList using add() method

 

import java.util.*;

 

public class GFG {

public static void main(String args[])

{

 

// create an ArrayList String type

ArrayList gfg = new ArrayList();

 

// Initialize an ArrayList with add()

gfg.add("21st");

gfg.add("century");

gfg.add("girl");

 

// print ArrayList

System.out.print("ArrayList : " + gfg);

}

}

 

OUTPUT:

 

ArrayList : [21st, century, girl]

 

Key Points About ArrayList

 

  • ArrayList inherits the AbstractList class and executes the List interface.
  • ArrayList is started by size; however, the size can grow if the collection increases or shrink if objects are removed from the collection.
  • Java ArrayList permits us to randomly access the list.
  • ArrayList cannot be used for primitive types like int, char, etc. We require a wrapper class for such cases.
  • In Java, an ArrayList is analogous to a vector in C++. 

 

Conversion of Array To ArrayList in Java

 

Here is a program to demonstrate the conversion of Array to ArrayList in Java of fixed size.

 

// Java program to demonstrate conversion of

// Array to ArrayList of fixed-size.

import java.util.*;

 

class GFG

{

public static void main (String[] args)

{

String[] geeks = {"Ram", "Ayush",

"Shivam", "Nandini"};

 

// Conversion of array to ArrayList

// using Arrays.asList

List al = Arrays.asList(geeks);

 

System.out.println(al);

}

}

 

OUTPUT:

 

[Ram, Ayush, Shivam, Nandini]

 

How Objects Can an ArrayList Hold in Java?

 

Here is a program to show how Objects Can an ArrayList Hold in Java

 

// Java Program to add elements to List

 

// Importing ArrayList class from

// java.util package

import java.util.ArrayList;

 

// Class

public class GFG {

 

// Mai driver method

public static void main(String[] args)

{

// Creating an ArrayList object

// (Declaring List of String type)

ArrayList names = new ArrayList();

 

// Adding (appending) elements to List

// Custom inputs using add() method

names.add(“Computer”);

names.add(“Science”);

names.add(“Portal”);

 

// Printing all the elements of ArrayList

// Declaring generic ArrayList of String type

System.out.print(names);

}

}

 

OUTPUT: 

 

[Computer, Science, Portal]

 

Conclusion

 

A dynamically sized collection of elements is stored by ArrayList in Java. Unlike arrays that are fixed in size, an ArrayList increases its size by itself when further elements are added to it. ArrayList is a component of Java’s collection framework and executes Java’s List interface.

 

full stack java developer course

 

 

About The Author:

logo

Digital Marketing Course

₹ 29,499/-Included 18% GST

Buy Course
  • Overview of Digital Marketing
  • SEO Basic Concepts
  • SMM and PPC Basics
  • Content and Email Marketing
  • Website Design
  • Free Certification

₹ 41,299/-Included 18% GST

Buy Course
  • Fundamentals of Digital Marketing
  • Core SEO, SMM, and SMO
  • Google Ads and Meta Ads
  • ORM & Content Marketing
  • 3 Month Internship
  • Free Certification
Trusted By
client icon trust pilot