Array and arraylist are well known data structures in Java programming language that are used to store the elements or objects. Arrays are basic functionality whereas Arraylists are collection frameworks in Java. 

What Is Array in Java?

  • Arrays in Java are known as dynamically created objects.
    Arrays can hold the values of elements in a constant manner. Array cannot change its size once created. Also, the memory location for the array is contiguous. 
  • To create an array with specific size, we can simply use command "int array[]=new int[size];" 
  • Array shows  a message "ArrayIndexOutofBoundsException" whenever we add more elements to it after the limit has exceeded.  
  • Whenever we use Arr(0), it will return the value of element 1. Most users often get confused whether it is for null element or element 1. Thus, users must have a deep understanding of memory storage while returning values in Arrays. 

Example of Array

int arr[]=new int[4]; 

//specified size of array is 4

//adding 5 elements into array 

arr[0]=13;

arr[1]=3;

arr[2]=16;

arr[3]=68;

arr[4]=90;

Output:

13

3

16

68

90

What Is Arraylist in Java?

  • Arraylist is not as strong or static as array and is concerned with the collection framework present in java.util package.
  • It can store similar as well as different data types. Thus, its overall size and quality might vary dynamically. Thus, it is dynamic in size. 
  • In C# collection, an ArrayList is one of the most important features to implement data structures in a simpler and easy manner. Also, it is able to implement Ilist Interface which is compatible with arrays.  
  • Ilist Interface is used to add, delete, insert, view or modify the data types. 
  • With the help of command "get()", one can easily access elements of ArrayList in Java. 
  • ArrayList is Java is also equivalent to C++ vector. 

Example of Arraylist

ArrayList Arrlst = new ArrayList ();

Arrlst.Add ("Delhi");

Arrlst.Add ("1");

Arrlst.Add ("null");

Difference Between Array and Arraylist

Array

  • Length of Array is static that means one cannot change its length that has been already defined by the developer to that particular element. This Array needs to specify the size of the elements.  
  • In other words, the length of the elements in the array is static or  requires more memory to store the elements and less time to iterate the elements. 
  • Array does not allow generics, though multidimensional in nature. 
  • In the array, giving references to objects or elements  depends upon the type of array such as primitive type or object type.
  • Functions such as indexOf() and remove() are not supported by Arrays in Java.

ArrayList

  • ArrayList uses the size() method to compute the size of the elements. Also, it is dynamic, which means one can change the size of the arraylist if the elements are modified in it, which means the length of the arraylist is variable. 
  • ArrayList requires more memory to store the elements as well as more time to iterate. 
  • ArraList enables the use of generic and single dimensional in nature. 
  • In arrayList, we can convert the primitive int data type into an Integer object with the help of commands such as “arraylist.add(1)” as shown in example. 
  • Since primitive data types can be created in ArrayList, the members of ArrayList are always given references to the  objects at every different memory locations . Thus, in ArrayList, the actual objects or elements are never stored at contiguous locations whereas their References can be stored at contiguous locations. 
  • In ArrayList, primitive types have actual values with contiguous locations, however object type allocation is similar to ArrayList. 
  • Operations such as indexOf(), remove() are supported by ArrayList in Java.

Array and ArrayList Program in Java to Demonstrate the Differences 

Base 1: On the basis of Functionality in Java

In Java, array is a basic functionality whereas ArrayList is a part of the collection framework. Array members can be accessed using [], while ArrayList can access elements using a set of methods and modify them.

Example 1: 

// Importing required classes

import java.util.ArrayList;

import java.util.Arrays;

// Main class

class GFG {   

    // Main driver method

    public static void main(String args[])

    {

        // Input array

        int[] arr = new int[2];

        arr[0] = 1;

        arr[1] = 2; 

        // Printing first element of array

        System.out.println(arr[0]); 

        // ArrayList

        // With initial capacity, create an arrayList 

        // say bi it 2

        ArrayList arrL = new ArrayList(2); 

        // Adding elements to ArrayList

        // using add() method

        arrL.add(1);

        arrL.add(2); 

        // Printing alongside accessing

        // elements of ArrayList

        System.out.println(arrL.get(0));

    }

}

Output: 

1

1

Base 2: On the Basis of Size of the Data Structure

The array is a fixed sized data structure thus, the array always needs to mention the size of the elements. On the other hand, ArrayList is not a fixed sized data structure, thus there is no need to mention the size of the ArrayList especially creating its object. Also,we can add more elements to Arraylist, though having some initial elements present in it.

Example:

// Java program to demonstrate differences between

// Array and ArrayList

// Importing required classes

import java.util.ArrayList;

import java.util.Arrays;

// Main class

class GFG {

    // Main driver method

    public static void main(String args[])

    {

        // Normal Array

        // The size for array need to be specified

        int[] arr = new int[3];

        arr[0] = 1;

        arr[1] = 2;

        arr[2] = 3; 

        // array arr[] cannot add more elements to it

        // ArrayList

        // Need not to specify size 

        //Declaration of Integer type in Arraylist 

        ArrayList arrL = new ArrayList(); 

        // Adding elements to ArrayList object

        arrL.add(1);

        arrL.add(2);

        arrL.add(3);

        arrL.add(4); 

        // Add more elements to arrL if required

        // Print and display Arraylist elements

        System.out.println(arrL);

        // Print and display array elements

        System.out.println(Arrays.toString(arr));

    }

}

Output:

[1, 2, 3, 4]

[1, 2, 3]      

Base 3: On the basis of Types of Data, Primitive or Object

An array has both primitive data types as well as object data types. However, ArrayList has only object-type data entries and not primitive data types.

Example: 

import java.util.ArrayList;

class Test

{

    public static void main(String args[])

    {

       // allowed

        int[] array = new int[3]; 

        // it is allowed and needs to be initialized as 

        Test[] array1 = new Test[3]; 

        //following commenting lines are not allowed that causes error

        // compiler error)

        // ArrayList arrL = new ArrayList(); 

        // Allowed

        ArrayList arrL1 = new ArrayList();

        ArrayList arrL2 = new ArrayList();

        ArrayList arrL3 = new ArrayList();       

        System.out.println("Successfully compiled and executed");

    }

}

Output: 

Successfully compiled and executed

Similarities

  1. Both are used to store the data elements as well as null elements 
  2. Both might have duplicate values, though they are not able to preserve the order of the elements 

Methods for Array

Arrays can be created with two different methods in Java. They are simple fixed sized arrays and dynamically sized arrays. 

Type 1- Simple Fixed Sized Array 

This array type can declare and initialize the elements at the same time. 

Syntax for Type 1:

array_name [array_size] ;

Type array_name = { Element1, 

Element2, 

Element3, 

Element4, 

Element5,

…….., 

ElementN } ;

// preferable for limited number (N) of array elements

Type 2- Dynamically Sized Array

This array type can declare the elements first and initialize them later. 

Syntax for Type 2:

int arr [200] ;

// 'arr' declares the memory block for elements 

// "arr" contains 200 continuous blocks associated with it.

Method for ArrayList 

Arraylist al = new ArrayList ;

// ArrayList to be created with the type of new elements.

Conclusion

I hope this article helped you understand Array and Arraylist and their differences. If you are looking to enhance your software development skills further, we highly recommend you check out Simplilearn’s Full Stack Java Developer Masters Program. This course, designed in collaboration with Caltech CTME, will help you sharpen your software development skills and make you job-ready in no time.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449

Get Free Certifications with free video courses

  • Introduction to C++

    Software Development

    Introduction to C++

    6 hours4.551.5K learners
  • Getting Started with Full Stack Development

    Software Development

    Getting Started with Full Stack Development

    12 hours4.551K learners
  • Full-Stack Development 101: What is Full-Stack Development ?

    Software Development

    Full-Stack Development 101: What is Full-Stack Development ?

    1 hours4.414.5K learners
prevNext

Learn from Industry Experts with free Masterclasses

  • Key 2025 Software Development Trends- Learn How To Leverage them for your career

    Software Development

    Key 2025 Software Development Trends- Learn How To Leverage them for your career

    9th Dec, Monday9:30 PM IST
  • Must-Know Full Stack Java Dev Career Trends for 2024

    Software Development

    Must-Know Full Stack Java Dev Career Trends for 2024

    6th Aug, Tuesday9:00 PM IST
  • Full Stack Java Development: A 2024 Blueprint for Recession-Proofing Your Career

    Software Development

    Full Stack Java Development: A 2024 Blueprint for Recession-Proofing Your Career

    27th Jun, Thursday7:30 PM IST
prevNext