Difference Between Similar Terms and Objects

Difference between Array and ArrayList

What is Array and ArrayList?

Both Array and ArrayList are index-based data structures that are often used in Java programs. Conceptually speaking, ArrayList is internally backed by arrays, however, understanding the difference between the two is the key to becoming a great Java developer. It’s quite the basic step to begin with, especially the beginners who’ve just started coding. While both are used to store elements in Java, which can be either primitives or objects, they have their fair share of differences in terms of functionality and performance. The major difference between the two is that Array is static whereas ArrayList is dynamic in nature. This article compares the two on various aspects so that you can choose one over the other.

Difference between Array and ArrayList

Difference between Array and Arraylist

  1. Resizable

One of the major and noticeable differences between the two data structures is that Array is static in nature which means it is a fixed length data type while ArrayList is dynamic in nature which means it’s a variable length data structure. In technical terms, the length of Array cannot be changed or modified once the Array object is created. It contains sequential collection of elements of the same data type. Arrays in Java work differently than they function in C/C++. ArrayList, on the other hand, can resize itself and the arrays can grow as they needed. As it is a dynamic data structure, the elements can be added and removed from the list.

  1. Generics

You cannot create Arrays of Generic classes of interfaces in Java so arrays and generics do not go hand in hand making it impossible to create Generic Array for the one basic reason that arrays are covariant while generics are invariant. While Array is a fixed-length data structure, it contains objects of the same class or primitives of the specific data type. So if you try to store different data type other than the one specified while creating Array object, it simply throws “ArrayStoreException”. ArrayList, on the other hand, does support Generics to ensure type-safety.

  1. Primitives

Primitive data types such as int, double, long, and char are not allowed in ArrayList. It rather holds objects and primitives are not considered objects in Java. Arrays, on the other hand, can hold primitives as well as objects in Java because it is one of the most efficient data structures in Java for storing objects. It’s an aggregated data type that is designed to hold objects which can be either of same or different type.

  1. Length

In order to get the length of the Array, the code needs to access the length attribute because one must know the length to perform operations on Array. While ArrayList uses size () method to determine the size of the ArrayList, it is rather different from determining the length of the Array. The size () method attribute determines the number of elements in an ArrayList, which in turn is the capacity of the ArrayList.

Difference between Array and ArrayList-1

For Example:

public class ArrayLengthTest {

public static void main(String[] args) {

ArrayList<String> arrList = new ArrayList<String>();

String[] items = { “One”, “Two”, “Three” };

for(String str: items){

arrList.add(str);

}

int size = items.size();

System.out.println(size);

}

}

  1. Implementation

Array is a native programming component in Java that are created dynamically and they use assignment operator to hold elements, while ArrayList use add( ) attribute to insert elements. ArrayList is a class from collection framework in Java which uses a set of specified methods to access and modify the elements. The size of an ArrayList can be increased or decreased dynamically. The elements in an array are stored in contiguous memory location and its size remains static throughout.

  1. Performance

While both the data structures provide similar kind of performance as an ArrayList is backed by Arrays, one has a little edge over another mostly in terms of CPU time and memory usage. Let’s say, if you know the size of the array, you’re likely to go with the ArrayList. However, iterating over an Array is a bit faster than iterating over an ArrayList. If the program involves a large number of primitives, an array will perform significantly better than the ArrayList, in terms of both time and memory. Arrays are a low-level programming language that can be used in collection implementations. However, performance may vary depending on the operation you’re performing.

Array Vs. Arraylist

Array ArrayList
Array is a fixed length data structure whose length cannot be modified once array object is created. ArrayList is dynamic in nature which means it can resize itself to grow when required.
The size of an array remains static throughout the program. The size of an ArrayList can grow dynamically depending on load and capacity.
It uses assignment operator to store elements. It uses the add() attribute to insert elements.
It can contain primitives as well as objects of same or different data type. Primitives are not allowed in ArrayList. It can only contain object types.
Arrays and Generics do not go hand in hand. Generics are allowed in ArrayList.
Arrays can be multi-dimensional. ArrayList is single dimensional.
It’s a native programming component where the elements are stored in contiguous memory locations. It’s a class from the collections framework of Java where the objects are never stored in contiguous locations.
Length variable is used to determine the length of the Array. Size () method is used to determine the size of the ArrayList.
Takes less memory than ArrayList to store specified elements or objects. Takes more memory than the Array to store objects.
Iterating over an array is faster than iterating over an ArrayList. Iterating over an ArrayList is significantly slower in terms of performance.

Summary

While some may think that implementing arrays in a program can get results faster than doing the same with ArrayLists for the simple reason that arrays are a low-level data structure, performance may vary based on what operation you’re performing. Well, the length of an Array is fixed, whereas the size of ArrayList can be increased or decreased dynamically, so ArrayList has a little edge over the Array in terms of functionality. However, despite the differences, they share some similarities too. Both are index-based data structures in Java that allow you to store objects and they both allow null values as well as duplicates. Well, if you know the size of the objects beforehand, you should go with an array, and if you’re not sure about the size, go with the ArrayList.

Latest posts by Sagar Khillar (see all)

Sharing is caring!


Search DifferenceBetween.net :




Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]Flanagan, David. Java in a Nutshell (5th ed.). Sebastopol: O’Reilly Media, 2005. Print

[1]Schildt, Herbert. Java: The Complete Reference. New York: McGraw-Hill Education, 2004. Print

[2]Sebesta, Robert W. Concepts of Programming Languages (7th ed.). Boston: Addison-Wesley, 2006. Print

[3]"Image Credit: https://stackoverflow.com/questions/20585702/find-smallest-integer-value-in-array-list-in-java-without-arrays-sort"

[4]"Image Credit: https://stackoverflow.com/questions/18580607/how-to-remove-elements-in-an-arraylist-start-from-an-indicated-index"

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : ,
Protected by Copyscape Plagiarism Finder