In this post, we will learn the difference between Array and ArrayList in Java. This is a frequently asked
question in Java interviews for beginners. Let's dive into it.
question in Java interviews for beginners. Let's dive into it.
Difference between Array and ArrayList in Java
Features | Array | ArrayList |
---|---|---|
Nature | Array is static in nature. Array is a fixed-length data structure. You can’t change their size once they are created. | ArrayList is dynamic in nature. Its size is automatically increased if you add elements beyond its capacity. |
Element Type | Array can hold both primitives as well as objects. | ArrayList can hold only objects. |
Iteration | Array can be iterated only through a for loop or for-each loop. | ArrayList provides iterators to iterate through their elements. |
Size Check | The size of an array is checked using the length attribute. | The size of an ArrayList can be checked using the size() method. |
Performance | Array gives constant time performance for both add and get operations. | ArrayList also gives constant time performance for both add and get operations provided adding an element doesn’t trigger resize. |
Generics Support | Array doesn’t support generics. | ArrayList supports generics. |
Type Safety | Array is a not type-safe. | ArrayList is type-safe. |
Dimensions | Array can be multi-dimensional. | ArrayList can’t be multi-dimensional. |
Element Addition | Elements are added using the assignment operator. | Elements are added using the add() method. |
Example
Using Array:
public class ArrayExample {
public static void main(String[] args) {
// Declare and initialize an array of integers
int[] numbersArray = {1, 2, 3, 4, 5};
// Modify an element in the array
numbersArray[2] = 99;
// Display elements of the array
System.out.println("Array:");
for (int num : numbersArray) {
System.out.print(num + " ");
}
}
}
Output:
Array:
1 2 99 4 5
Using ArrayList:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Declare and initialize an ArrayList of integers
ArrayList<Integer> numbersList = new ArrayList<>();
// Add elements to the ArrayList
numbersList.add(1);
numbersList.add(2);
numbersList.add(3);
numbersList.add(4);
numbersList.add(5);
// Modify an element in the ArrayList
numbersList.set(2, 99);
// Remove an element
numbersList.remove(4); // removes the element at index 4
// Display elements of the ArrayList
System.out.println("ArrayList:");
for (int num : numbersList) {
System.out.print(num + " ");
}
}
}
Output:
ArrayList:
1 2 99 4
Download Cheat Sheet:
Related Collections Interview QA
- map() vs flatMap() in Java
- Collections vs Streams
- ArrayList vs Vector
- Iterator vs ListIterator
- HashMap vs HashTable
- HashSet vs HashMap
- Array vs ArrayList
- Fail-Fast Iterators vs Fail-Safe Iterators
- HashMap vs ConcurrentHashMap
- LinkedList vs ArrayDeque
- LinkedList vs Array
- LinkedList vs Doubly LinkedList
- Enum vs EnumSet in Java
- HashMap vs. TreeMap in Java
- Synchronized Collections vs. Concurrent Collections
Collection Framework
Interview Questions
Java
X vs Y
Comments
Post a Comment