LATEST UPDATES

Friday 18 November 2016

What is difference between Array and ArrayList ? When will you use Array over ArrayList ?

  • Arrays can contain primitive or objects, while an ArrayList can contain only objects.
  • Arrays have fixed size, while an ArrayList is dynamic.
  • An ArrayListprovides more methods and features, such as addAllremoveAlliterator, etc.
  • For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this approach makes them slower when working on fixed size primitive data types.

Array
ArrayList
Arrays are data structures that can store elements of similar data type.
ArrayList is a collection that can store elements of similar data type as well as elements of different data type.
Along with objects, Arrays can also store elements of primitive data type.
ArrayList cannot store elements of primitive data type. ArrayList can store only objects.
Array size is fixed.
ArrayList is resizable. Its size is not fixed.
While declaring and creating the Array, the size is specified in square brackets []. For example,
int[] intArray = new int[10]
The above statement creates an integer array of size 10. After creating it, its size cannot be changed. It cannot hold more than 10 elements.
ArrayList has a special parameter called initialCapacity using which the initial size of the ArrayList is specified. Later on, the ArrayList can be resized.
Array cannot automatically grow or shrink in size.
ArrayList has the capability to automatically grow and shrink in size. When an element is removed from the ArrayList, its size is reduced by 1. When an element is added to the ArrayList and if its initial capacity is reached then the size is automatically increased by 1.
Array is not part of collection framework. Arrays are directly defined in java language as a basic data structure.
ArrayList is part of Collection framework.
Arrays inherit from java.lang.Object.
ArrayList implements List interface and inherits AbstractList class.
Generics cannot be used along with Arrays.
When elements of similar data type have to be stored in an ArrayList then the ArrayList will use generics.
Arrays can be single dimensional or multi dimensional.
ArrayList can only be single dimensional.
Arrays are present in java.lang package. However a customized array class is present in java.util package. This java.util.Arrays has the capability to perform binary search, copy of another array, fill, sort and much more using its predefined methods.
ArrayList is present in java.util package. ArrayList also has rich predefined methods such as add, addAll, remove, toArray, iterator, listiterator, sort, reverse and much more.
Arrays can be iterated through a simple for loop by iterating over its index.
ArrayList can be iterated using Iterator or ListIterator.
Arrays cannot be synchronized.
ArrayList can be synchronized.
Array elements are accessed using its index. The index ranges from 0 to array length-1.
ArrayList elements are accessed using get() and other specialized methods.

No comments:

Post a Comment

Designed By Blogger Templates