Writing Our Own ArrayList
This lab will give you a peek under the hood of the ArrayList class, by asking you to write your own generic ArrayList class
Although our class is generic, recall arrays of generic types are not allowed in Java.
For more information, see Lesson notes on Generics.
Therefore, we will need to store an array of Objects and cast to the generic type E when needed.
For example, here should be your default constructor:
public ArrayList() {
array = (E[]) new Object[10]; //declare a new array of Objects, but cast to type E[]
numElements = 0;
}
Note: Generic code can generate warnings when casting to a generic type (as in the above method).
There is no way for the compiler to verify that the types are compatible, when the type is unknown
We will suppress the warnings, using @SuppressWarnings("unchecked"), as we are confident that no runtime errors will occur
I have added the tags for you in the starter code below in any case that they are required
Copy and paste the following starter code into a new file called ArrayList.java:
/**
* ArrayList.java
* @author
* @author
* CIS 36B, Lab 9
*/
public class ArrayList<E> {
private E[] array;
private int numElements;
/**
* Default constructor for ArrayList
* Creates an empty array of length 10
* Sets numElements to 0
*/
@SuppressWarnings("unchecked")
public ArrayList() {
array = (E[]) new Object[10];
numElements = 0;
}
/**
* Constructor for ArrayList
* Creates an empty array of length size
* Sets numElements to 0
* @param size the initial size of the
* ArrayList
*/
@SuppressWarnings("unchecked")
public ArrayList(int size) {
}
/**
* Copy constructor for ArrayList
* Creates a new list array of the
* same size as the ArrayList passed
* in as a parameter, and copies the
* parameter's data into the new
* ArrayList using a for loop
* Also sets the numElements to be
* the same as the parameter's
* numElements
* @param la the ArrayList to copy
*/
@SuppressWarnings("unchecked")
public ArrayList(ArrayList<E> la) {
}
/**
* Returns whether the ArrayList is
* currently empty
* @return whether the ArrayList is
* empty
*/
public boolean isEmpty() {
return false;
}
/**
* Returns the current number of
* elements stored in the ArrayList
* @return the number of elements
*/
public int size() {
return -1;
}
/**
* Returns the element at the specified
* index. Throws an exception if index is
* larger than or equal to numElements
* @param index the index of the element
* to access
* @return the element at index
*/
public E get(int index) throws IndexOutOfBoundsException{
return null;
}
/**
* Uses the linearSearch algorithm to
* locate an element in the ArrayList
* @param element the element to locate
* @return whether or not the element
* is in the ArrayList
*/
public boolean contains(E element) {
return false;
}
/**
* Uses the linearSearch algorithm to
* locate an element in the ArrayList
* @param element the element to locate
* @return the location of the element
* or -1 if the element is not in the
* ArrayList
*/
public int indexOf(E element) {
return -1;
}
/**
* Determines whether the ArrayList
* is currently full
* @return whether the ArrayList
* is at maximum capacity
* Should be called by the add methods
*/
private boolean atCapacity() {
return false;
}
/**
* Resizes the ArrayList by making a new
* array that has a length (capacity)
* 10 larger than the current array's
* length (capacity)
*/
@SuppressWarnings("unchecked")
private void reSize() {
}
/**
* Inserts a new element to the end
* of the list array.
* Resizes the ArrayList if it is
* at capacity before inserting
* @param element the element to insert
*/
public void add(E element) {
}
/**
* Inserts a new element at the specified
* index in the ArrayList.
* Resizes the ArrayList if it is
* at capacity before inserting
* @param index the index at which to insert
* @param element the element to insert
*/
public void add(int index, E element) throws IndexOutOfBoundsException {
}
/**
* Assigns a new value to the ArrayList
* at a specified index
* @param index the index at which to update
* @param element the new element
*/
public void set(int index, E element) throws IndexOutOfBoundsException {
}
/**
* Removes an element at a specified index in
* the ArrayList
* @param index the index at which to remove
* @return the element that was removed
*/
@SuppressWarnings("unchecked")
public E remove(int index) throws IndexOutOfBoundsException{
return null;
}
/**
* Removes the first instance of the specified
* element in the list array
* @param str the element to remove
* @return whether the element was successfully
* removed
*/
public boolean remove(E element) {
return false;
}
/**
* Returns whether two ArrayLists and
* the same length and store
* the same data in the same order
*/
@SuppressWarnings("unchecked")
@Override public boolean equals(Object o) {
return false;
}
/**
* Creates a String of all elements,
* with [] around the elements,
* each element separated from the next
* with a comma
*/
@Override public String toString() {
String result = "[";
return result + "]";
}
}
Please do not import ArrayList in either your ArrayList.java or ArrayListTest.java or you will receive a 0 for this lab.
Next, write each method according to the description in the comment.
As you write each method, test it using the test file.
Make sure that one method is working before you move on to write the next <--good coding practice!
This assignment has been answered 2 times in private sessions.
Or buy a ready solution below.
© 2024 Codify Tutor. All rights reserved