Have a Question?

If you have any question you can ask below or enter what you are looking for!

What is an Array and difference between array and collection?

What is an Array and difference between array and collection?

Limitations of arrays 

  • Arrays are fixed in size that is once we create an array there is no chance of increasing or decreasing the size based on our requirement. Due to this to use arrays concept compulsory we should know the size in advance which may not possible always.
  • An array can hold only homogeneous data type elements.
public class Test {
    public static void main(String[] args) {
        Test t[] = new Test[10];
        t[0] = new Test();
        t[1] = new Employee();  //CE:- incompatable found
    }
}
  • We can solve this problem by using object type arrays.
public class Test {
    public static void main(String[] args) {
        Object obj[] = new Object[10];
        obj[0] = new Test();
        obj[1] = new Employee();
    }
}
  • Arrays concept is not implemented based on some standard data structure and hence ready-made method support is not available. For every requirement, we have to write the code explicitly which increases the complexity of programming.
  • To overcome above problems of arrays we should go collections concept.
    • Collections are growing able in nature that is based on our requirement we can increase or decrease the size.
    • Collections can hold both homogeneous and heterogeneous object.
    • Every collection class is implemented based on some standard data structure. Hence for every requirement readymade method support is available. Being a programmer we are responsible to use those methods and we are not responsible to implement that method.

Differences between Arrays and Collections 

No Array Collections
1 Arrays are fixed in size that is once we creates an arrays we can’t increase and decrease the size based on our requirement. Collections are grow able in nature that is based on our requirement we can increase or decrease the size.
2 With respect to memory arrays are not recommended to use. With respect to memory collections are recommended to use.
3 With respect to performance arrays are recommended to use. With respect to performance collections are not recommended to use.
4 Arrays can hold only homogeneous data type elements. Collections can hold both homogeneous and heterogeneous elements.
5 There is no under lying data structure for arrays and hence readymade method support is not available, for every requirement we have to write the code explicitly which increase complexity of programing. Every collection class is implemented based on some standard data structure and hence for every requirement readymade method support is available, being a programmer we can use these method directly and we are not responsible to implement those methods.
6 Arrays can hold both primitives and objects. Collections can hold only object types but not primitives.

Leave a Reply

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