Sunday, 1 January 2012

Difference between Collections and Arrays

Arrays:
  • An Array is an indexed collection of fixed number of  homogeneous data elements.
  • The main advantage of Array is we can represent huge no of elements by using single variable.so that readability of code will be improved   

 Limitation of Object Arrays:  

Arrays are fixed in size i.e once we create an array  there is no chance of   increasing or  decreasing   the size based on our requirement . To use Array concept compulsory we should know the size advance.which may not possible always
         
               Arrays can hold homogeneous data elements.
EX:

                           Student[] s = new Student[20] ;

                           s[0]=new   Student();

                           s[1] =new Customer();//compile time error:In compatible types. 

                                                                  found:customer    
                                                                  required:student                        

         But we can resolve this problem by using Object type Arrays

                            Object[] obj=new Object[20];

                            obj[0]=new   Student();

                            obj[1] =new Customer();


Array concept not implemented based on some data structures.Hence ready made support methods we can't expect.  so every requirement we have to write the code explicitly.


COLLECTIONS:
  • If we want represent the group a group of objects as single entity then   we should go for collections.  
  • Collections can hold both  homogenous and heterogeneous objects. 
  •  Collections are growable in nature.i.e based on our requirement we can increase or decrease the size.Hence memory point of Collection concept is recommended to use.  
  • Every collection class implemented based in some data structures,hence every requirement ready made methods support is available.Programmer can use this methods directly without writing the functionality   on our own.      
  Difference  between Arrays and Collections:

           ARRAYS                                                             COLLECTIONS
1.  Arrays are fixed in size                                                      1. collections are growable in nature

2.Memory point of view Arrays are not recommended       2.Memory point of view        
    Collections                                                                               recommended.

                   
3.Performance point of view Arrays                                      3.Performance point of Collections    
   are are  recommended                                                              not recommended        

4.Arrays can hold homogenous data elements                      4.collections can hold both 
                                                                                                      homogenous and heterogeneous  
                                                                                                      elements




5.There is no underlying data structure for Arrays             5.Every collection class is
                                                                                                       implemented  based on some data                                                                                                            structure                                                                                                                                                                                       

6.Arrays can hold both primitive and objects types              6.Collections can hold only Objects















         

                                      
                        

    1 comment:

    1. Hello Sir

      The article is nice. As we know that ARRAYS and COLLECTIONS are two wrapper collection present in java.util package. and what I know that , this article is describing the different between "ARRAY" and "COLLECTION" and a lot of different between "ARRAYS" and "ARRAY" as well "COLLECTION" and "COLLECTIONS".So,please change the name "ARRAYS" to "ARRAY" and "COLLECTIONS" to "COLLECTION".

      ReplyDelete