Difference between Array and Collection in java

What you will learn here about Java

  • Difference between Array and Collection in java

Difference between Array and Collection in java

The most important key Differences between array and collection in java are given below
ParameterArrayCollection
1)SizeArray is fixed in sizeCollection is dynamic in size
2)DataArray can store only homogeneous data onlyCollection can store homogeneous as well as hetrogeneous data
3)Data StorageArray can store primtive as well as objectsCollections stores only object
4)Data StructureArray does not have any standard data structure so no ready made methods availableCollection supports standard data structure so ready made methods are available
5)MemoryMemory wise array is worst choiceMemory wise collection is best choice
6)PerformancePerformance wise array is good choicePerformance wise collection is worst choice

Array example in java

public class ArrayvsCollection {

	public static void main(String[] args) 
	{
		//Example of array
		int a[]= {0,2,1,4,5}; //only integer can store
		System.out.println(a.length);

	}

}
	

Collection example in java

import java.util.ArrayList;

public class ArrayvsCollection {

	public static void main(String[] args) 
	{
		//Example of collection
		ArrayList elements=new ArrayList();
		elements.add(1);
		elements.add("Hello");
		elements.add(100);
		System.out.println(elements);

	}

}
	

You may also like...