Introduction
The AbstractList class in the Java Collections Framework provides a skeletal implementation of the List interface, enabling developers to create custom list implementations with ease. It offers default implementations for some list operations, allowing focus on specific aspects of the custom list. On the other hand, the ArrayList class is a concrete implementation of the List interface that uses a dynamic array to store elements. Known for its resizable nature and efficient index-based access, ArrayList is ideal for scenarios requiring frequent access and modification of elements. While AbstractList serves as a base for custom list creation, ArrayList provides a versatile and practical solution for dynamic list management.
The AbstractList Class
The AbstractList class extends the AbstractCollection class and implements the List interface, allowing for random access to data. For example, it enables the use of index values to access elements in an array at random. The constructor for this class is represented as: protected AbstractList(). The AbstractList class extends methods such as add(Object o), clear(), and iterator() from the AbstractCollection class, and it also inherits methods from the List interface.
Source Code
import java.util.AbstractList;
public class SimpleList<E> extends AbstractList<E> {
private Object[] elements;
private int size;
public SimpleList(int capacity) {
elements = new Object[capacity];
size = 0;
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
return (E) elements[index];
}
@Override
public int size() {
return size;
}
@Override
public boolean add(E e) {
if (size == elements.length) {
ensureCapacity(size + 1);
}
elements[size++] = e;
return true;
}
private void ensureCapacity(int minCapacity) {
int oldCapacity = elements.length;
if (minCapacity > oldCapacity) {
int newCapacity = oldCapacity * 2;
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
Object[] newElements = new Object[newCapacity];
System.arraycopy(elements, 0, newElements, 0, size);
elements = newElements;
}
}
public static void main(String[] args) {
SimpleList<String> myList = new SimpleList<>(5);
myList.add("Apple");
myList.add("Banana");
myList.add("Cherry");
for (int i = 0; i < myList.size(); i++) {
System.out.println(myList.get(i));
}
}
}
Output

The provided code defines a custom list implementation named SimpleList by extending the AbstractList class in Java. The SimpleList class uses an internal array to store elements and supports dynamic resizing. It includes a constructor that initializes the list with a specified capacity and methods to add elements (add(E e)), retrieve elements by index (get(int index)), and determine the size of the list (size()). The ensureCapacity(int minCapacity) method is used to resize the internal array when needed. The main method demonstrates the usage of SimpleList by creating an instance, adding a few string elements, and printing them. This example highlights how to build a basic, dynamically resizable list using the AbstractList class.


Join the conversation! Your thoughts help the community grow.