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
Parameter | Array | Collection |
---|
1)Size | Array is fixed in size | Collection is dynamic in size |
2)Data | Array can store only homogeneous data only | Collection can store homogeneous as well as hetrogeneous data |
3)Data Storage | Array can store primtive as well as objects | Collections stores only object |
4)Data Structure | Array does not have any standard data structure so no ready made methods available | Collection supports standard data structure so ready made methods are available |
5)Memory | Memory wise array is worst choice | Memory wise collection is best choice |
6)Performance | Performance wise array is good choice | Performance 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);
}
}