SringSring

Tuesday, 28 October 2014

Difference between Array vs ArrayList in Java

1) Array is a fixed length data structure. ArrayList is a variable length Collection class.
2) You can not use Generics along with Array, as Array instance knows about what kind of type it can hold and throws ArrayStoreException, if you try to store type which is not convertible into type of Array. ArrayList allows you to use Generics to ensure type-safety.
3) All kinds of Array provides length variable which denotes length of Array while ArrayList provides size() method to calculate size of ArrayList.
4) You can not store primitives in ArrayList, it can only contain Objects. While Array can contain both primitives and Objects in Java.
5) Create instance of ArrayList without specifying size, Java will create Array List with default size but its mandatory to provide size of Array while creating either directly or indirectly by initializing Array while creating it.

No comments:

Post a Comment