Creating a simplified version of ArrayList
involves implementing dynamic array behavior with essential methods like add
, get
, size
, and handling dynamic resizing when the capacity is exceeded. Here's a basic implementation of an ArrayList
-like data structure in Java:
import java.util.Arrays;
public class MyArrayList<E> {
private static final int DEFAULT_CAPACITY = 10;
private Object[] elements;
private int size;
// Constructor
public MyArrayList() {
elements = new Object[DEFAULT_CAPACITY];
size = 0;
}
// Add element to the end of the list
public void add(E element) {
if (size == elements.length) {
// Resize the array if full
increaseCapacity();
}
elements[size++] = element;
}
// Get element at specified index
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
return (E) elements[index];
}
// Return size of the list
public int size() {
return size;
}
// Increase the capacity of the internal array
private void increaseCapacity() {
int newCapacity = elements.length * 2;
elements = Arrays.copyOf(elements, newCapacity);
}
// Main method for testing
public static void main(String[] args) {
MyArrayList<Integer> list = new MyArrayList<>();
// Adding elements
list.add(1);
list.add(2);
list.add(3);
// Accessing elements
System.out.println("Element at index 0: " + list.get(0));
System.out.println("Element at index 1: " + list.get(1));
// Size of the list
System.out.println("Size of the list: " + list.size());
}
}
Explanation:
Instance Variables:
elements
: Array to store elements of typeObject
.size
: Current number of elements in the list.
Constructor:
- Initializes the
elements
array with a default capacity (DEFAULT_CAPACITY
). - Initializes
size
to 0.
- Initializes the
add
Method:- Appends the specified element to the end of the list.
- Checks if the internal array is full (
size == elements.length
), and if so, callsincreaseCapacity()
to double the array size.
get
Method:- Retrieves the element at the specified index.
- Throws an
IndexOutOfBoundsException
if the index is out of range (index < 0 || index >= size
).
size
Method:- Returns the current number of elements in the list (
size
).
- Returns the current number of elements in the list (
increaseCapacity
Method:- Doubles the capacity of the
elements
array usingArrays.copyOf()
when the array is full.
- Doubles the capacity of the
main
Method:- Example usage demonstrating adding elements, accessing elements, and getting the size of the list.
Considerations:
- This implementation does not handle all edge cases or provide full functionality of the Java
ArrayList
class (e.g., iterators, removal methods, etc.). - It serves as a basic introduction to how dynamic array resizing and element addition can be implemented in Java.
To fully replicate the functionality of java.util.ArrayList
, additional methods and considerations for concurrency, iterators, removal operations, and other functionalities would need to be implemented.