Array List
Array List
Ali Hassan
Array List
• An ArrayList is a dynamic array-like data structure that is part of the
Java Collections Framework. It allows you to store and manipulate a
collection of elements, and it automatically handles the resizing of the
underlying array as elements are added or removed.
Importing ArrayList
• To use ArrayLists in your Java program, you need to import the
java.util package
import java.util.ArrayList;
Declaration and Initialization
• You can declare and initialize an ArrayList like this
OR
listName.add(element);
Accessing Elements
• You can access elements by their index using the get() method
listName.remove(index);
listName.remove(element);
Size
• You can get the number of elements in an ArrayList using the size()
method
OR
Iterator<Type> iterator = listName.iterator();
while (iterator.hasNext()) {
Type element = iterator.next();
// Do something with the element
}
Checking if Empty
• You can check if an ArrayList is empty using the isEmpty() method
listName.clear();
ArrayList Capacity
• Internally, an ArrayList uses an array to store elements. If the array
becomes full, it is automatically resized to accommodate more
elements. You can specify the initial capacity of an ArrayList when you
create it, but it will grow as needed.
ArrayList is Generic
• ArrayList is a generic class, which means you can specify the type of
elements it can hold. For example, ArrayList<Integer> can only hold
integers.
Array VS Array List
1. Array can be multi-dimensional. 1. ArrayList is always single-dimensional.
2. Array is static in size. 2. ArrayList is dynamic in size
3. It performs fast in comparison to 3. ArrayList is internally backed by the
ArrayList because of fixed size array in Java. The resize operation in
ArrayList slows down the
performance.
4. An array can store both objects and
primitives type. 4. We cannot store primitive type in
ArrayList. It automatically converts
primitive type to object.
5. We use for loop or for each loop to 5. We use an iterator to iterate over
iterate over an array. ArrayList.
Advantages
• Dynamic Sizing: ArrayLists can dynamically resize themselves to accommodate more
elements as needed. You don't need to specify the size in advance.
• Ease of Use: ArrayLists provide simple and intuitive methods for adding, accessing,
and removing elements. This makes them easy to work with, especially for beginners.
• Random Access: Elements in an ArrayList can be accessed by their index, allowing for
efficient random access. This is useful when you need to retrieve elements at specific
positions quickly.
• Generics Support: ArrayLists are generic, meaning they can hold elements of a
specific type, providing compile-time type safety.
• Collection Framework Integration: ArrayLists are part of the Java Collections
Framework, making them compatible with other collection classes and interfaces.
This simplifies operations like sorting and searching.
• Iterating Elements: ArrayLists can be easily iterated using enhanced for loops,
iterators, or other iteration methods, making it convenient to process elements
Disadvantages
• Performance Overhead: ArrayLists have a performance overhead compared to arrays due to dynamic
resizing. When the internal array needs to be resized, it involves copying elements, which can be costly
for large lists.
• Wasted Space: The internal array of an ArrayList may be larger than the actual number of elements,
resulting in wasted memory. This overhead can be significant if the initial capacity is set too high.
• Inefficient Removal: Removing an element from the middle or beginning of an ArrayList can be
inefficient, as it requires shifting all subsequent elements to fill the gap. This operation has a time
complexity of O(n).
• Not Suitable for Primitive Types: ArrayLists can only hold objects, not primitive data types (e.g., int,
double). This means that primitive types need to be boxed into their respective wrapper classes (e.g.,
Integer, Double), which can lead to additional memory overhead.
• No Built-in Synchronization: ArrayLists are not thread-safe by default. If you need to use them in a
multi-threaded environment, you need to manually synchronize access or use a synchronized wrapper
like Collections.synchronizedList().
• Limited Performance Guarantees: While ArrayLists provide good performance for most use cases,
there are situations where other data structures like LinkedLists or HashSet/TreeSet may perform
better depending on the specific operations you need to perform (e.g., frequent insertions/removals in
the middle of the list).