ArrayList memory allocation
Array vs ArrayList in JavaLet us discuss the concept of the arrays and ArrayList briefly in the header to incorporate the understanding in java programs later landing onto the conclusive differences between them. As we all are aware of that arrays are linear data structures providing functionality to add elements in a continuous manner in memory address space whereas ArrayList is a class belonging to the Collection framework. Being a good programmer one is already aware of using ArrayList over arrays despite knowing the differences between these two. Now moving ahead even with ArrayList there comes a functionality to pass the type of datatype of elements that are supposed to be stored in the ArrayList be it an object, string, integer, double, float, etc.
Methods of Creating Arrays In Java, the following are two different ways to create an array.
Syntax: Declaring a static array array It can be further defined by two types:
Type 1 Type array_name [array_size] ; Type array_name = { Element1, Element2, Element3, Element4,...., ElementN } ; // It is preferable if we have very limited array elementsType 2
Now let us dwell on the next concept of ArrayList that is as follows Syntax: Declaring an Arraylist Arraylist
Now let us illustrate examples with the help of differences between Array and ArrayList Base 1: An array is a basic functionality provided by Java. ArrayList is part of the collection framework in Java. Therefore array members are accessed using [], while ArrayList has a set of methods to access elements and modify them. Example: Java
Output
1
1
Base 2: The array is a fixed-size data structure while ArrayList is not. One need not mention the size of the ArrayList while creating its object. Even if we specify some initial capacity, we can add more elements. Example: Java
Output
[1, 2, 3, 4]
[1, 2, 3]
Base 3: An array can contain both primitive data types as well as objects of a class depending on the definition of the array. However, ArrayList only supports object entries, not the primitive data types.
Example: Java
Output
Successfully compiled and executed
Base 4: Since ArrayList cant be created for primitive data types, members of ArrayList are always references to objects at different memory locations (See this for details). Therefore in ArrayList, the actual objects are never stored at contiguous locations. References of the actual objects are stored at contiguous locations. On the other hand, in the array, it depends whether the array is of primitive type or object type. In the case of primitive types, actual values are contiguous locations, but in the case of objects, allocation is similar to ArrayList. Java ArrayList supports many additional operations like indexOf(), remove(), etc. These functions are not supported by Arrays. We have implemented and seen the differences between them as perceived from outputs. Now let us wrap up the article by plotting conclusive differences in a tabular format a shown below as follows:
This article is contributed by Pranjal Mathur. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Article Tags :
Java
Java - util package Java-Array-Programs Java-ArrayList Java-Arrays Java-Collections Java-List-Programs Practice Tags :
Java Java-Collections
Read Full Article
|