Implementing a Contiguous List with Dynamic Array Operations

A contiguous list relies on an array as its underlying storage. The elemnets occupy consecutive memory locations, and the logical order matches the physical layout. Unlike a plain array that may need sentinel values to determine usage, this structure tracks element count through an explicit size variable. The following sections walk through a practical implementation covering fundamental list manipulations in Java.


Internal Representation

public class ArrayListImpl {
    private int[] data;
    private int count;
    private static final int DEFAULT_CAPACITY = 10;

    public ArrayListImpl(int initialCapacity) {
        this.data = new int[initialCapacity];
    }

    public ArrayListImpl() {
        this.data = new int[DEFAULT_CAPACITY];
    }
}

Displaying Elements

public void printAll() {
    for (int idx = 0; idx < count; idx++) {
        System.out.print(data[idx] + " ");
    }
    System.out.println();
}

Insert Operations

Before inserting, we verify capacity. If the array is full, we grow it.

private void ensureCapacity() {
    if (count == data.length) {
        data = Arrays.copyOf(data, data.length * 2);
    }
}

Append Element

public void append(int value) {
    ensureCapacity();
    data[count] = value;
    count++;
}

Insert at Specific Index

public void insertAt(int index, int value) {
    validateInsertIndex(index);
    ensureCapacity();
    for (int i = count - 1; i >= index; i--) {
        data[i + 1] = data[i];
    }
    data[index] = value;
    count++;
}

private void validateInsertIndex(int index) {
    if (index < 0 || index > count) {
        throw new IndexOutOfBoundsException("Invalid position: " + index);
    }
}

Query Operations

public boolean contains(int target) {
    checkNotEmpty();
    for (int i = 0; i < count; i++) {
        if (data[i] == target) {
            return true;
        }
    }
    return false;
}

public int findIndex(int target) {
    checkNotEmpty();
    for (int i = 0; i < count; i++) {
        if (data[i] == target) {
            return i;
        }
    }
    return -1;
}

public int elementAt(int index) {
    validateAccessIndex(index);
    checkNotEmpty();
    return data[index];
}

public int currentSize() {
    return count;
}

private void checkNotEmpty() {
    if (count == 0) {
        throw new IllegalStateException("List is empty.");
    }
}

private void validateAccessIndex(int index) {
    if (index < 0 || index >= count) {
        throw new IndexOutOfBoundsException("Invalid index: " + index);
    }
}

Update Operation

public void updateAt(int index, int newValue) {
    validateAccessIndex(index);
    checkNotEmpty();
    data[index] = newValue;
}

Delete Operations

Remove First Occurrence

public void removeValue(int target) {
    int pos = findIndex(target);
    if (pos == -1) {
        throw new NoSuchElementException("Value not found: " + target);
    }
    for (int i = pos; i < count - 1; i++) {
        data[i] = data[i + 1];
    }
    count--;
}

Clear Entire List

public void clearAll() {
    count = 0;
}

Tags: Data Structures java Contiguous List Dynamic Array

Posted on Wed, 09 Sep 2026 16:50:00 +0000 by SevereSoldier