Tuesday 17 November 2015

Array vs ArrayList


Array
ArrayList
Size
Array is a fixed length data structure.

Length of array cannot changed once it created.
ArrayList is a variable length Collection class.
Resizing operation in ArrayList is slow down performance as it creates new Array and copying content from old Array to new Array.

Generics
Array doesn’t support the Generics, as Array instance knows about what kind of type it can hold and throws ArrayStoreException.

ArrayList allows you to use Generics to ensure type-safety.

Wrapper
Array allows to store both primitives and Objects in Java.
ArrayList doesn’t allow to store primitives in ArrayList, it can only contain Objects.

ArrayList<Integer> integerList = new ArrayList<Integer>();

integerList.add(1);
Primitive data type will be autobox into the Integer object while storing.

Size
Initialize
We can create instance of ArrayList without specifying size.

It is mandatory to provide size of Array while creating.
Multi-dimensional
Array can be multi-dimensional.

Array list is single dimensional.
Performance
Fast.

Slow because resizing.
Iterator
Using for, foreach
Using foreach, Iterator, List Iterator.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...