Array is an object and it stores the object of same type
Eg : It stores complete integer types or string types
int[] MyIntArray = new int[3];
string[] MyStringArray = new MyStringArray[2];
Arraylist is a collection in which we can store objects of any data type.
Eg : Arraylist al = new Arraylist()
al.Add(13); // Integer Type
al.Add(10); // String Type
2. Arrays have Fixed Length whereas Arraylist varies in leghth as the objects are Added.
In Java an Array is always of fixed size. The size has to be defined at the initializing time of the array. The number of the rows is a must for any array, though the number of columns may be specified later. By using Array, there is comparatively less flexibility, as the size need to be known before hand. And if you initialize an array that is too long, then it results in wastage of precious memory of the heap. The Garbage Collector then has a tough time maintaining the memory for other resources.
On the other hand, the ArrayList is dynamic in nature. It provides for automatic resizing of the List. Moreover you need not specify the size at the beginning of the initialization part. The ArrayList is therefore much more widely used as compared to an Array in Java.
No comments:
Post a Comment