Array vs ArrayList in Java Learn with Examples

1. Array Vs ArrayList in Java – Objective

In this Java Array tutorial, we are going to learn about what is Java Array and ArrayList in Java. Moreover, we will also discuss Array vs ArrayList performance in Java. By this “Difference Between Array and ArrayList in Java”, we will also learn when to use Array and ArrayList in Java.

So, let’s start Array vs ArrayList in Java.

Array Vs ArrayList in Java

Array vs ArrayList in Java Learn with Examples

2. Array and ArrayList in Java

There are basically two ways to create Array in Java:

  • Java Array
int arr[] = new int[10]
  • ArrayList Java
ArrayList<Type> arrL = new ArrayList<Type>();

Here, Type is the type of elements in ArrayList to be created.

Do you know What is StringBuffer In Java?

3. Differences Between Array and ArrayList in Java

  • Basically, an Array is fundamental usefulness gave by Java. ArrayList is a piece of collection framework in Java. In this way, exhibit individuals are gotten to utilizing [], while ArrayList has an arrangement of techniques to get to components and adjust them.

Example –

import java.util.ArrayList;
import java.util.Arrays;
class Test
{
   public static void main(String args[])
   {
       int[] arr = new int[2];
       arr[0] = 1;
       arr[1] = 2;
       System.out.println(arr[0]);
       ArrayList<Integer> arrL = new ArrayList<Integer>(2);
       arrL.add(1);
       arrL.add(2);
       System.out.println(arrL.get(0));
   }
}

Output –
1
1

  • The exhibit is a settled size information structure while ArrayList isn’t. One need also the measure of ArrayList while making its object. Regardless of whether we indicate some underlying limit, we can include more components.

Java Extends vs Implements With Example Program

Join DataFlair on Telegram

Example –

import java.util.ArrayList;
import java.util.Arrays;
class Test
{
   public static void main(String args[])
   {
       int[] arr = new int[3];
       arr[0] = 1;
       arr[1] = 2;
       arr[2] = 3;
       ArrayList<Integer> arrL = new ArrayList<Integer>();
       arrL.add(1)
       arrL.add(2);
       arrL.add(3);
       arrL.add(4);
       System.out.println(arrL);
       System.out.println(Arrays.toString(arr));
   }
}

Output –
[1, 2, 3, 4]
[1, 2, 3]

  • An array can contain both primitive information writes and in addition objects of a class contingent upon the meaning of the exhibit. In any case, ArrayList just backings protest sections, not the primitive information composes.

Note: When we do arraylist.add(1);: it changes over the primitive int information write into an Integer object. Test Code:

Example –

import java.util.ArrayList;
class Test
{
   public static void main(String args[])
   {
       int[] array = new int[3];
       Test[] array1 = new Test[3];
       ArrayList<Integer> arrL1 = new ArrayList<>();
       ArrayList<String> arrL2 = new ArrayList<>();
       ArrayList<Object> arrL3 = new ArrayList<>();
   }
}
  • Since ArrayList can’t be made for primitive information writes, individuals from ArrayList are dependably references to objects at various memory areas. In this manner in ArrayList, the real objects are never put away at coterminous areas. References of the genuine items are put away at adjacent areas. In a cluster, it depends whether the exhibits are of a primitive kind or protest write. If there should arise an occurrence of primitive sorts, genuine qualities are touching areas, however, in the event of articles, an assignment is like ArrayList.

So, this was all about Array vs ArrayList in Java. Hope you like our explanation of the difference between Java Array and ArrayList.

Let’s Learn Java Annotations in Detail 

Java Quiz

4. Conclusion

Hence, in this Java tutorial, we learned the basic difference between Array and ArrayList in Java. Moreover, we compare Java Array vs ArrayList with the example and understand it with the help of Java codes. Furthermore, for any query regarding Java Array vs ArrayList, feel free to ask in the comment section.
For reference-

 

No Responses

  1. Kumail says:

    kindly explain more on this topic

    • Data Flair says:

      Hi Kumail,
      Glad to see your comment that you want to explore Java array more. We have already published the detailed Java Array Tutorial. You can take help of this tutorial. I am sure, you will going to like this article. Here is the link for you
      https://data-flair.training/blogs/java-array/
      If you want us to explain any other topic please let us know. We will happy to help you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.