In real time programming , Some times we need to group individual objects into single unit . We can achieve this using arrays . But arrays have below limitations . Those are :
Limitations of arrays :
1) Array is used to hold collection of similar type data.
Employee emp[]= new Employee[3];
emp[0] = new Employee(); ( ✅ )
emp[1] = new Employee(); ( ✅ )
emp[2] = new Student(); ( ✖ ) // causes compiler error. Allows Employee type objects only
We can resolve the above problem by defining the Object type . But we will face problems while retrieving the data from array .
Object emp[]= new Object[3];
emp[0] = new Employee(); ( ✅ )
emp[1] = new Employee(); ( ✅ )
emp[2] = new Student(); ( ✅ ) // Now Allows. While retrieving, we will face problems
2) Array is fixed in size. That means once we define array, we can’t change it’s size dynamically .
Object emp[]= new Object[2];
emp[0] = new Employee(); ( ✅ )
emp[1] = new Employee(); ( ✅ )
emp[2] = new Employee(); ( ✖ ) // Allows two objects only.
3) Arrays concept is not built based on any data structures . There are no sufficient supportive methods of arrays to arrange , manipulate and retrieve data as a single unit . Developer have to write logic for his needs .
Two over come above limitations of arrays, java people provided set of interfaces and classes , together called collection framework .