Limitations of an Array
- The major drawback for an array is , arrays are fixed in size. There’s no point of increasing the size of an array after its creation.
- Therefore , the programmers should know the size of an array in advance which is quite complex in real time programming.
- Arrays are homogeneous , i.e arrays holds only one type of data. An integer array cannot hold a string value and vice versa.
- In Java arrays don’t have any underlying data structure , therefore arrays does not have any pre-defined methods for easy programming.
- Another major drawback of an array is , sorting of elements in an array must be done explicitly which is quite hard coded. But in collections the sorting can be done by simply importing the pre defined methods for sorting purposes.
- Like sorting , searching for an element is also done by explicitly.
Array size limitation
For Example :
int numbers = new int; numbers = 100; numbers = 200; numbers = 300; numbers = 400; numbers = 500; numbers = 600; // which is not possible because the size of the array is fixed.
But in Collections Framework this is possible. In Collections framework there is a class called ArrayList where the size can be increased during the run time.
ArrayList<int> al = new Arraylist<int>(); //here the default size is 10 and can be increased during the runtime
Array homogeneous limitation
- Arrays are homogeneous in nature , one int type of array variable cannot hold a string value and vice versa.
For Example , there are two classes which are called 'Student' and 'Teachers' Creating an object array for Student class Student s = new Student; //creates an object of size 100 s = new Student(); // this is possible because of the same type s = new Teacher(); // this is not possible because Teacher is of different type
- But this can be resolved by using Object array
Object obj = new Object; obj = new Student(); obj = new Teacher();
Array Manipulation Limitation
The elements in arrays are stored according to the indexes , and those elements are accessed with the help of index value. Indexes are helpful in searching an element and placing and element. But in arrays there is no pre-defined methods for searching and customized sorting techniques as found in Collections Framework.