C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Compare array and ArrayList in Java

Learn: Compare array and ArrayList in java? How differentiate array and ArrayList in java? Array v/s ArrayList in java? Difference between array and ArrayList in java? How array differ from ArrayList in java?
Submitted by Preeti Jain, on January 31, 2018

1) Arrays in Java

1) 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 

2) 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).

3) 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).

4) 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

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

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

Example 1: 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 2: For Primitives Type

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

Output

E:\javasource>java ArrayClass
0 

2) ArrayList in java

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


Liked this article? Do share with your friends :)


COMMENTS