Header Ads Widget

Responsive Advertisement

Custom ArrayList By Java Source code

 

Creating a custom ArrayList in Java involves implementing basic functionality similar to Java's built-in ArrayList class. Below is a simplified version of a custom ArrayList implementation:

Custom ArrayList Implementation

java

import java.util.Arrays;

import java.util.NoSuchElementException;

 

public class CustomArrayList<E> {

    private static final int DEFAULT_CAPACITY = 10;

    private Object[] elements;

    private int size;

 

    // Default constructor

    public CustomArrayList() {

        elements = new Object[DEFAULT_CAPACITY];

        size = 0;

    }

 

    // Add element to the end of the list

    public void add(E element) {

        ensureCapacity();

        elements[size++] = element;

    }

 

    // Get element at specified index

    public E get(int index) {

        checkIndex(index);

        return (E) elements[index];

    }

 

    // Set element at specified index

    public void set(int index, E element) {

        checkIndex(index);

        elements[index] = element;

    }

 

    // Remove element at specified index

    public void remove(int index) {

        checkIndex(index);

        int numMoved = size - index - 1;

        if (numMoved > 0) {

            System.arraycopy(elements, index + 1, elements, index, numMoved);

        }

        elements[--size] = null; // clear to let GC do its work

    }

 

    // Get the size of the list

    public int size() {

        return size;

    }

 

    // Check if the list is empty

    public boolean isEmpty() {

        return size == 0;

    }

 

    // Ensure capacity of the internal array

    private void ensureCapacity() {

        if (size == elements.length) {

            int newCapacity = elements.length * 2;

            elements = Arrays.copyOf(elements, newCapacity);

        }

    }

 

    // Check if index is within bounds

    private void checkIndex(int index) {

        if (index < 0 || index >= size) {

            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);

        }

    }

 

    // Main method for testing

    public static void main(String[] args) {

        CustomArrayList<String> list = new CustomArrayList<>();

       

        list.add("Apple");

        list.add("Banana");

        list.add("Cherry");

 

        System.out.println("Size: " + list.size()); // Output: Size: 3

 

        System.out.println("Element at index 1: " + list.get(1)); // Output: Element at index 1: Banana

       

        list.set(1, "Blueberry");

        System.out.println("Element at index 1 after set: " + list.get(1)); // Output: Element at index 1 after set: Blueberry

       

        list.remove(1);

        System.out.println("Size after remove: " + list.size()); // Output: Size after remove: 2

 

        System.out.println("Element at index 1 after remove: " + list.get(1)); // Output: Element at index 1: Cherry

    }

}

 

Key Components

  1. Internal Array: The elements array holds the list items. It's initially created with a default capacity and resized when needed.
  2. Size: The size variable tracks the number of elements in the list.
  3. Add Method: Adds an element to the end of the list, expanding the internal array if necessary.
  4. Get Method: Retrieves the element at the specified index, checking for bounds.
  5. Set Method: Replaces the element at the specified index with a new one, checking for bounds.
  6. Remove Method: Removes the element at the specified index and shifts subsequent elements to fill the gap.
  7. Capacity Management: The ensureCapacity method ensures that there is enough space in the internal array by doubling its size if necessary.
  8. Index Checking: The checkIndex method ensures that operations do not go out of bounds.

This implementation provides a basic overview of how ArrayList operations can be managed, including resizing and element access. You can extend this with additional methods and optimizations as needed.

 

Custom ArrayList
Custom ArrayList



Post a Comment

3 Comments

Kartik Chandra Mandal
ccc said…
Excellent !great one!!..
Kartik Chandra Mandal
ccc said…
Simple logic and super logic
Kartik Chandra Mandal
Unknown said…
Good work.
Functionality that can be added is provide an option to declare the size at time of creating an object.