Array and ArrayList

Array and ArrayList

Array and ArrayList

Array and ArrayList are data structures but with lot of differences.

Array

Array is a collection of similar data types. The data stored is known as elements. Elements added are automatically indexed. The first element is given zero and the elements can be retrieved by their index numbers. Retrieving the elements is very faster with index numbers. A far away element from the beginning can be obtained quickly. Arrays give high performance in data retrieval; but the minus points are they can store only one type of data and cannot be resized. The memory for array is allocated in contiguous locations. It is an overhead to OS. The OS should search for sufficient memory in the RAM and if the memory is not available in continuous locations, pagination (creating space by pushing occupied memory to another location) should be done and then allocated. An array can be multidimensional. To use array, no package is to be imported.

ArrayList

ArrayList is a data structure from collections framework. All the methods of Collection interface like add(), remove(), toArray() can be used by ArrayList. Also being derived from List interface, it can also use the methods of List like subList(), indexOf(), listIterator() etc. ArrayList stores variables of same data type or different data types and also objects; stores all in the form of objects internally. ArrayList increases its size dynamically when more and more elements are added at runtime. To use ArrayList, the package java.util is to be imported.

ArrayList is more like a vector where ArrayList methods are not synchronized.

ArrayList is more useful to store non-numeric data. When the data size is not known, ArrayList is preferable as size increases dynamically. One more advantage is elements can be inserted anywhere.

class Arrays

To get confused, there exists a class in java.util package by name Arrays. Arrays is a class derived from Object and comes with many static methods with which array elements can be manipulated like searching, sorting etc.

Leave a Reply

Your email address will not be published. Required fields are marked *