Question
Complete the implementation of the ArrayList class. /* * To change this license header, choose License Headers in Project Properties. * To change this template
Complete the implementation of the ArrayList class.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jsjf;
import jsjf.exceptions.*; import java.util.*;
/** * ArrayList represents an array implementation of a list. The front of * the list is kept at array index 0. This class will be extended * to create a specific kind of list. * * @author Lewis and Chase * @version 4.0 */ public abstract class ArrayList
/** * Creates an empty list using the default capacity. */ public ArrayList() { this(DEFAULT_CAPACITY); }
/** * Creates an empty list using the specified capacity. * * @param initialCapacity the integer value of the size of the array list */ public ArrayList(int initialCapacity) { rear = 0; list = (T[])(new Object[initialCapacity]); modCount = 0; }
/** * Creates a new array to store the contents of this list with * twice the capacity of the old one. Called by descendant classes * that add elements to the list. */ protected void expandCapacity() { // To be completed as a Programming Project } /** * Removes and returns the last element in this list. * * @return the last element in the list * @throws EmptyCollectionException if the element is not in the list */ public T removeLast() throws EmptyCollectionException { // To be completed as a Programming Project }
/** * Removes and returns the first element in this list. * * @return the first element in the list * @throws EmptyCollectionException if the element is not in the list */ public T removeFirst() throws EmptyCollectionException { // To be completed as a Programming Project }
/** * Removes and returns the specified element. * * @param element the element to be removed and returned from the list * @return the removed elememt * @throws ElementNotFoundException if the element is not in the list */ public T remove(T element) { T result; int index = find(element);
if (index == NOT_FOUND) throw new ElementNotFoundException("ArrayList");
result = list[index]; rear--; // shift the appropriate elements for (int scan=index; scan < rear; scan++) list[scan] = list[scan+1]; list[rear] = null; modCount++;
return result; } /** * Returns a reference to the element at the front of this list. * The element is not removed from the list. Throws an * EmptyCollectionException if the list is empty. * * @return a reference to the first element in the list * @throws EmptyCollectionException if the list is empty */ public T first() throws EmptyCollectionException { // To be completed as a Programming Project }
/** * Returns a reference to the element at the rear of this list. * The element is not removed from the list. Throws an * EmptyCollectionException if the list is empty. * * @return a reference to the last element of this list * @throws EmptyCollectionException if the list is empty */ public T last() throws EmptyCollectionException { // To be completed as a Programming Project }
/** * Returns true if this list contains the specified element. * * @param target the target element * @return true if the target is in the list, false otherwise */ public boolean contains(T target) { return (find(target) != NOT_FOUND); }
/** * Returns the array index of the specified element, or the * constant NOT_FOUND if it is not found. * * @param target the target element * @return the index of the target element, or the * NOT_FOUND constant */ private int find(T target) { int scan = 0; int result = NOT_FOUND; if (!isEmpty()) while (result == NOT_FOUND && scan < rear) if (target.equals(list[scan])) result = scan; else scan++;
return result; }
/** * Returns true if this list is empty and false otherwise. * * @return true if the list is empty, false otherwise */ public boolean isEmpty() { // To be completed as a Programming Project } /** * Returns the number of elements currently in this list. * * @return the number of elements in the list */ public int size() { // To be completed as a Programming Project }
/** * Returns a string representation of this list. * * @return the string representation of the list */ public String toString() { // To be completed as a Programming Project } /** * Returns an iterator for the elements currently in this list. * * @return an iterator for the elements in the list */ public Iterator
/** * ArrayListIterator iterator over the elements of an ArrayList. */ private class ArrayListIterator implements Iterator
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started