CS-151 Labs >Lab 3. My ArrayList


Part 1. MyArrayList Basics

You’ll be constructing your very own implementation of the Array List so that you better understand its inner workings. You should use the prefix My in your class names so that you don’t accidentally test the standard ArrayList in your testing. However, you will be matching the behaviour of java.util.ArrayList, so when in doubt, you can refer back to the ArrayList documentation for clarification and use the ArrayList as a working reference solution.

Your MyArrayList class will implement the Java List interface. That is, you will need to provide implementation for all of the (abstract) methods contained therein. There are more than 20 such methods, however, and that could take you a while to get through. Moreover, some of the methods are “redundant” in the sense that they can be implemented using the other methods (for example, you can implement isEmpty() by returning (size() == 0). Fortunately, Java provides a lovely abstract class AbstractList that implements some very basic and default behavior for a List. Some of the methods still aren’t implemented (that is, they are abstract) and some of the concrete methods may have inefficient implementation (that is, you’ll want to override them), but it’s useful nonetheless. Thus your MyArrayList class should extend AbstractList in order to reap the benefits. Because AbstractList implements the List interface, MyArrayList will as well.

You’ll be using generics for your implementation, taking advantage of the fact that List and AbstractList are similarly parameterized.

In the lab3 project create a new class called MyArrayList<E> with package lab3 and superclass java.util.AbstractList<E>. AbstractList<E> gives you implementations for all methods except for get(int index) and size(); however, you’ll need to override many other methods to get your MyArrayList<E> in fighting form.

Basic Design

As you might expect from the name, the backing storage for an ArrayList is an array. That is, an ArrayList is really just an array with the extra functionality of dynamic resizing. The MyArrayList can change its size; therefore, your class will need to keep track of the number of items that are currently stored in the underlying array. This size is not the same as the length field of the underlying array. You need to keep the data in the array packed to the front so there are no gaps.

Private instance variables

Add the following members to your MyArrayList class.

private int size
number of items currently stored in the list (which may differ from the list’s current capacity)
private E[] data
backing storage for MyArrayList

Constructors

Implement the following constructors in your MyArrayList class.

MyArrayList(int initialCapacity)
initialCapacity is the initial number of slots in the underlying array.
This number will be doubled when the array becomes full.

We can create our initial array with the following line:

E[] someArray=(E [])new Object[initialCapacity];

You will get a compiler warning about assigning an untyped array to a generic type. This is normal behavior, as this could lead to type safety problems if it was done accidentally. Remember, one of the jobs of the compilers is to highlight risky behavior! In our case, though, we know that the cast is safe, but since Java does not integrate regular arrays well with Generics, the warning is shown.

Good programming practice is to either have code without warnings or to supress the warnings that we know are ok. Here we can solve the warning issue by using a special marker in the source code. Place this line right above the Constructor header, outside the method itself:

@SuppressWarnings("unchecked")

This will get rid of the corresponding warning message in Eclipse.

MyArrayList()
By default use an initial capacity of 2. NOTE: you should not be duplicating code from your other constructor in this one. Have this constructor call the other with appropriate arguments. You can do this by executing the line this(2); in your new constructor. This will call your base constructor with the argument ‘2’.

Private Methods

You will need to implement a private resize() method that doubles the size of the array (do not just increase its length by one) by creating a new array of twice the current size, copying the data over, then resetting the data pointer to point to the new array.

private void resize()
  1. Create a new array that is twice the size of the previous (use data.length).
  2. Copy all of the old data into the new array
  3. Set this.data to be your new array

Feel free to write more private methods as you see fit.

Public Methods (part 1. More to follow.)

Implement the following methods in your MyArrayList class.

int size()
Return the number of items stored in the array, not the maximum capacity (i.e, not the size of the underlying data array that you get from data.length)
void add(int index, E element)
Adds the element at the specified index, shifting everything else right one step. You are allowed to add at any index location up to and including the current size (index==size is the first unused location). Resize the array if necessary. Throw an IndexOutOfBoundsException if the index is not within the allowed range. Remember, an Exception is just an object, so you create it just like you do a regular object. For example, you may try the following:
throw new IndexOutOfBoundsException();

or even better:

throw new IndexOutOfBoundsException("Index Out of Bounds! You tried to get " +
    index + " but the size is " + size );

Or something to that effect.

boolean add(E element)
Adds an item to the end of the array. (Hint: you can call the previous method—don’t just copy the code from there.) This method returns true if the item was added. Since you will always be adding the item specified, you should always return true.
E get(int index)
Return the value stored at the given index. Throw an IndexOutOfBoundsException if the index is not within the allowed range. Note that this range is smaller than the range allowed for add().

Javadoc Comments

Good API documentation is very important if you want somebody to use your classes. Fortunately, all modern versions of the JDK provide the Javadoc tool – for generating API documentation from comments present in the source code.

You write comments directly in the source file, and then you compile them into an HTML file using a special Javadoc tool. After adding your javadoc comments to MyArrayList.java you can try compiling them following instructions here.

In this lab you will start learning how to properly add Javadoc comments to your project. Javadoc comments look very similar to a regular multi-line comment, but the key difference is the extra asterisk at the beginning:

// This is a single line comment

/*
 * This is a regular multi-line comment
 */

/**
 * This is a Javadoc
 */

In the beginning of each class, we add the class-level Javadoc comments:

 /**
* MyArrayList is ... used for ...
* 
* @author John Doe
* 
*/

Before each method, we add the following method-level comments:

/**
 * <p>This is a simple description of the method. . .
 * </p>
 * @param e element of type T to be added to the end ...
 * @return success or failure of the add operation
 */
public boolean add(T e) {
    // do things
    return true;
}

Starting with this lab, you are required to add Javadoc comments to all your classes and public methods.

Good programming style suggestions

In your resize() method, we’d suggest printing yourself a message when it is called at first indicating what size it is and what size it becomes. Just be sure to remove it after doing some testing.

Also, take advantage of your existing methods and don’t duplicate logic. You’ve got two constructors and two add() methods. Only one of each needs to do the real work, the second should just figure out a way to call the first.

You should include a meaningful message when you construct the IndexOutOfBoundsException. (For example, when Java’s ArrayList throws an IndexOutOfBoundsException, it tells you which index was accessed and the size of the ArrayList. This seems like a useful information.)