Title: MyArrayList implementing List
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

MyArrayList implementing List interface


import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MyArrayList<E> implements List<E>{
    Object[] container;
    int numElements =0;

    public MyArrayList(int initialSize)
    {
        container = new Object[initialSize];
    }
    public MyArrayList()
    {
        this(10);
    }

    public int size()
    {
        return numElements;
    }
    public boolean add(E ob)
    {
        if (numElements >= container.length)
        {
            resize();
        }
        container[numElements++] = ob;
        return true;
    }
    public E get(int index)
    {
        if (index < 0 || index >= numElements)
            throw new IndexOutOfBoundsException("Index out of bounds");
        return (E)container[index];
    }

    private void resize()
    {
        int currSize = container.length;
        Object[] nextContainer = new Object[currSize*2];
        for (int i=0; i < numElements; i++)
            nextContainer[i] = container[i];

        container = nextContainer;
    }
    public E set(int index, E ob)
    {
        if (index < 0  || index >= numElements)
            throw new IndexOutOfBoundsException("Index out of bounds: "+ index);
        E replacedObject = (E)container[index];
        container[index] = ob;

        return replacedObject;
    }
    // Inserts object at desired location
    public void add(int index, E ob)
    {
        if (index < 0  || index > numElements)
            throw new IndexOutOfBoundsException("Index out of bounds: "+ index);
        add(ob); // This will increase our size by 1
        // Shift objects to make room for the new addition
        for (int i= numElements-1; i > index; i--)
        {
            container[i] = container[i-1];
        }
        container[index] = ob;
    }

    // For this to work, the objects need to implement equals
    public int indexOf(Object ob)
    {
        for (int i=0; i < numElements; i++)
        {
            if (ob.equals(container[i]))
                return i;
        }
        return -1;
    }

    public E remove(int index)
    {
        if (index < 0  || index >= numElements)
            throw new IndexOutOfBoundsException("Index out of bounds: "+ index);

        // Shift everything down
        E removedObject = (E)container[index];
        for (int i= index; i < numElements-1; i++)
        {
            container[i] = container[i+1];
        }
        numElements -= 1;
        return removedObject;
    }

    // for this to work, the objects need to implement equals
    public boolean remove (Object ob)
    {
        int index = indexOf(ob);
        if (index < 0)
            return false;

        remove(index);
        return true;
    }

    public String toString()
    {
        StringBuilder sb= new StringBuilder();
        sb.append("[ ");
        for (int i=0; i < numElements; i++)
        {
            sb.append(container[i].toString());
            if (i < numElements-1)
                sb.append(",");
        }
        sb.append(" ]");
        return sb.toString();
    }
    @Override
    public boolean addAll(Collection<? extends E> c) {
        throw new UnsupportedOperationException();
    }
    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        throw new UnsupportedOperationException();
    }
    @Override
    public void clear() {
        throw new UnsupportedOperationException();

    }
    @Override
    public boolean contains(Object o) {
        throw new UnsupportedOperationException();
    }
    @Override
    public boolean containsAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }
    @Override
    public boolean isEmpty() {
        throw new UnsupportedOperationException();
    }
    @Override
    public Iterator<E> iterator() {
        throw new UnsupportedOperationException();
    }
    @Override
    public int lastIndexOf(Object o) {
        throw new UnsupportedOperationException();
    }
    @Override
    public ListIterator<E> listIterator() {
        throw new UnsupportedOperationException();
    }
    @Override
    public ListIterator<E> listIterator(int index) {
        throw new UnsupportedOperationException();
    }
    @Override
    public boolean removeAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }
    @Override
    public boolean retainAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }
    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        throw new UnsupportedOperationException();
    }
    @Override
    public Object[] toArray() {
        throw new UnsupportedOperationException();
    }
    @Override
    public <T> T[] toArray(T[] a) {
        throw new UnsupportedOperationException();
    }

}



Last Updated: October 23, 2013 2:10 AM