Java - Array and ArrayList Comparison

By Preeti Jain Last updated : February 03, 2024

Java Array

Arrays are fixed in size (i.e. we can't increase or decrease size at runtime).

Example

class ArrayClass {
  public static void main(String[] args) {
    int[] a = new int[10];
    System.out.println(a[0]);
  }
}

Output

E:\javasource>java ArrayClass
0 

In case of memory point of view arrays concept is not recommended to use (i.e. arrays size are fixed if we use memory for elements less than arrays size so memory will be wasted).

In case of performance point of view arrays concept is recommended to use (i.e. we know the size of arrays in advance or at compile time so no overhead at runtime that why it takes less time).

Arrays can hold homogeneous data elements (i.e. Array elements are of same type).

Example

class ArrayClass {
  public static void main(String[] args) {
    int[] a = new int[10];
    a[0] = new boolean[10];
    System.out.println(a[0]);
  }
}

Output

E:\javasource>javac ArrayClass.java
ArrayClass.java:8: incompatible types
found   : boolean[]
required: int
                        a[0]    = new boolean[10];
                                  ^
1 error

Arrays don't provide readymade method support that's why we can call as arrays is not underlying data structure.

Arrays are capable to hold both primitives (byte, short, int, long etc.) and Objects (Wrapper Classes, String, StringBuffer or Any user defined classes).

Example: For Object Type

class ArrayClass {
  public static void main(String[] args) {
    Integer[] a = new Integer[10];
    System.out.println(a[0]);
  }
}

Output

E:\javasource>java ArrayClass
null

Example: For Primitive Types

class ArrayClass {
  public static void main(String[] args) {
    int[] a = new int[10];
    System.out.println(a[0]);
  }
}

Output

E:\javasource>java ArrayClass
0 

Java ArrayList

ArrayList are growable in nature (i.e.we can increase or decrease size at runtime).

Example

import java.util.*;

class ArrayListClass {
  public static void main(String[] args) {
    ArrayList al = new ArrayList(2);
    al.add(10);
    al.add(20);
    System.out.println(al);
    al.add(30);
    System.out.println(al);
  }
}

Output

E:\javasource>java ArrayListClass
[10, 20]
[10, 20, 30]

In case of memory point of view ArrayList is recommended to use (i.e. ArrayList size are not fixed memory will be allocated according to ArrayList elements size).

In case of performance point of view ArrayList is not recommended to use (i.e. we don't know the size of ArrayList in advance or at compile time let suppose initially memory is allocated for 10 elements As 11th element then again new memory will be allocated and all elements will be copied in new memory).

ArrayList can hold both homogeneous and heterogeneous data elements (i.e. ArrayList elements may be of different type).

Example

import java.util.*;
class ArrayListClass {
  public static void main(String[] args) {
    ArrayList al = new ArrayList(10);
    al.add("A");
    al.add("true");
    System.out.println(al);
  }
}

Output

E:\javasource>java ArrayListClass
[10, true]

ArrayList provide readymade method support that's why we can call as ArrayList is underlying data structure.

Example

import java.util.*;
class ArrayListClass {
  public static void main(String[] args) {
    ArrayList al = new ArrayList(10);
    al.add("A");
    al.add("true");
    al.remove(1);
    System.out.println(al.size());
  }
}

Output

add(), remove(), size() etc. are the readymade methods .

E:\javasource>java ArrayListClass
1

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.