2
\$\begingroup\$
public class ArrayList<E> implements List<E> {
    private static final int defaultMaxSize = 10;
    private int maxSize;
    private int size;
    private int currentPosition;
    private E[] array;

    /**
     * Create a new ArrayList object.
     *
     * @param maxSize
     */
    @SuppressWarnings("unchecked")
    public ArrayList(int maxSize) {
    this.maxSize = maxSize;
    this.size = this.currentPosition = 0;
    this.array = (E[]) new Object[this.maxSize];
    }

    /**
     * Create a new ArrayList object.
     */
    public ArrayList() {
    this(defaultMaxSize);
    }

    @Override
    public void insert(E item) {
    assert this.size < this.maxSize : "ArrayList capacity exceeded";
    for (int i = this.size; i > this.currentPosition; i--) {
        // Shift elements up to make room
        this.array[i] = this.array[i - 1];
    }
    this.array[this.currentPosition] = item;
    this.size++;
    }

    @Override
    public void append(E item) {
    assert this.size < this.maxSize : "ArrayList capacity exceeded";
    this.array[this.size++] = item;
    }

    @Override
    public E remove() {
    if ((this.currentPosition < 0) || (this.currentPosition >= this.size)) {
        return null;
    }
    E item = this.array[this.currentPosition];
    for (int i = this.currentPosition; i < this.size - 1; i++) {
        this.array[i] = this.array[i + 1];
    }
    this.size--;
    return item;
    }

    @Override
    public void clear() {
    this.size = this.currentPosition = 0;
    }

    @Override
    public void moveToStart() {
    this.currentPosition = 0;
    }

    @Override
    public void moveToEnd() {
    this.currentPosition = this.size;

    }

    @Override
    public boolean previous() {
    if (this.currentPosition != 0) {
        this.currentPosition--;
        return true;
    } else {
        return false;
    }
    }

    @Override
    public boolean next() {
    if (this.currentPosition < this.size) {
        this.currentPosition++;
        return true;
    } else {
        return false;
    }
    }

    @Override
    public int length() {
    return this.size;
    }

    @Override
    public int currentPosition() {
    return this.currentPosition;
    }

    @Override
    public void moveCurrentToPosition(int position) {
    assert (position >= 0) && (position <= this.size) : "position is out of range";
    this.currentPosition = position;
    }

    @Override
    public E getValue() {
    assert (this.currentPosition >= 0) && (this.currentPosition < this.size) :
        "No current element";
    return this.array[this.currentPosition];
    }
}

And here is the List interface!

public interface List<E> {
    /**
     * Insert an element behind the current position. Must check that the linked
     * list's capacity is not exceeded.
     *
     * @param item
     *            Item to be inserted.
     */
    public void insert(E item);

    /**
     * Insert an element after the last element in the list.
     *
     * @param item
     *            Item to be appended.
     */
    public void append(E item);

    /**
     * Remove the element after the current element and return the value of the
     * removed element.
     *
     * @return The element that was removed.
     */
    public E remove();

    /**
     * Remove all contents from the list.
     */
    public void clear();

    /**
     * Move current position to first element.
     */
    public void moveToStart();

    /**
     * Move current position to last element.
     */
    public void moveToEnd();

    /**
     * Move the current position one element before. No change if already at the
     * beginning.
     *
     * @return True if moved to previous position; otherwise return false.
     */
    public boolean previous();

    /**
     * Move the current position one element after. No change if already at the
     * end.
     *
     * @return True if moved to current position; otherwise return false.
     */
    public boolean next();

    /**
     * @return The number of items in the list.
     */
    public int length();

    /**
     * @return The current position.
     */
    public int currentPosition();

    /**
     * @param position
     *            Position to move current to.
     */
    public void moveCurrentToPosition(int position);

    /**
     * @return The current item in the current position.
     */
    public E getValue();
}
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$
  • First and foremost : why are you writing this class? It doesn't look like an improvement or a specialized version of java.util.ArrayList.
  • the insert() contract states : "Must check that the linked list's capacity is not exceeded." (wait, linked list?). You implement this with an assert. Yet asserts can be disabled. Don't use assert to honor the contract. It also is worth mentioning that the contract is unclear about what to do when the capacity is insufficient.
  • remove(), moveToStart(), moveToEnd(), previous(), next(), getValue() and currentPosition() are things that belong on a ListIterator. This allows for more flexibility - having multiple iterators at the same time for instance.
  • Since the underlying array does not grow when the original capacity is insufficient, your class adds little value compared to a naked array.
  • naming wise the documentation talks about capacity, yet the implementation names it maxSize, it's better to name this field consistent with the documentation. There is a similar mismatch with 'length' and 'size'.
  • insert() manually shifts elements, instead of using the more efficient System.arrayCopy() - the same goes for remove().
\$\endgroup\$
3
  • \$\begingroup\$ I'd assume he's writing it just as a fun exercise for himself, not in an attempt to improve the existing API. :) \$\endgroup\$
    – asteri
    Oct 4, 2013 at 23:05
  • \$\begingroup\$ Ya it's just an exercise. \$\endgroup\$
    – letter Q
    Oct 5, 2013 at 0:38
  • \$\begingroup\$ In that case, why don't you implement the java.util.List interface? \$\endgroup\$
    – bowmore
    Oct 5, 2013 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.