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. 

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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. 

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

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

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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]      

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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.

Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!

Conclusion

Hope this article was able to give you a clear understanding of Array and Arraylist and the differences between them. If you are looking to further enhance your software development skills, then we would highly recommend you to check Simplilearn’s Post Graduate Program in Full Stack Web Development. This course, designed in collaboration with Caltech CTME, will help you sharpen your software development skills and make you job-ready in no time.

If you have any questions or doubts, feel free to post them in the comments section below. Our team will get back to you with the solutions at the earliest.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.