Sunday, May 5, 2019

Array Vs ArrayList In Java

Q- What is the difference between Array and ArrayList?

                            Array                                 ArrayList
    Array is a fixed-length data structure.
    That means we cannot change the length     of Array once created.
    ArrayList is a variable-length Collection class.
    ArrayList re-size itself when gets full depending upon     the capacity and load factor.
    Array can contain both primitives and     Objects     We cannot store primitives in ArrayList.
    Only object can store. Though Autoboxing of Java 5     we can store primitives in ArrayList, it actually     automatically converts primitives to Object.
    ArrayList can create an instance of     ArrayList without specifying size. It will     create Array List with default size     It is mandatory to provide the size of
    Array while creating an instance.
    Performs faster than ArrayList because it is     strongly typed.     Performs slows because of boxging and unboxing.
    Generics are not compatible with Arrays.     ArrayLists allow the use of Generics.
    Arrays can be multi-dimensional.     ArrayLists are single-dimensional only.
    The Length variable is used to determining     an Array’s length.     Size() method is used to get the length of an ArrayList.
    Arrays take more memory for the storage     of specified
    objects or elements.
    ArrayLists take less memory space for storing objects     or elements.
    Iterating on Array takes less time then     ArrayList.     Iterating on ArrayList takes more time, hence     performance is slower.

No comments:

Post a Comment