1.

Difference between Array and ArrayList in Java.

Answer»
DefinitionAn Array is a collection of similar data types STORED in contiguous memory locations.An ArrayList is a class of Java Collections framework which contains popular classes like Vector, HashMap, etc.
Static/ Dynamicstatic in SIZE.dynamic in size.
ResizableAn array is not resizable as it is a fixed-length data structure.An ArrayList is a variable-length data structure that can be resized. As an element is being added to an ArrayList, JVM checks to see if it has enough space by calling the ensureCapacity method. If a space exists, it adds the element to the ArrayList, otherwise, it resizes the ArrayList.In the resizing process, an array of a larger size is created, the old array is COPIED to the new array using Arrays.copyOf, and the new array is then assigned to the existing array.
InitializationSize of an array should be declared directly or indirectly while initializing. An instance of ArrayList can be created without specifying its size. Java creates an ArrayList of default size.
PerformanceIt is fast in comparison to ArrayList as it has a fixed size.Slow as the resize operation in ArrayList slows down the performance.
Primitive/ Generic typeCan store both objects and primitives type.Arraylist automatically converts primitive type to object.
Iterating ValuesA for loop or for each loop is used to iterate over an array.An iterator is used to iterate over ArrayList.
LengthIt provides a length variable that DENOTES the length of an array.size() method can be used to DETERMINE the size of ArrayList.
Single/ Multi-DimensionalCan be multi-dimensional.Single-dimensional.


Discussion

No Comment Found