Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your Own Array-Based List Class Write your own array-based list class from scrat

ID: 3848677 • Letter: Y

Question

Your Own Array-Based List Class

Write your own array-based list class from scratch, using generics. You can either implement the GeneralList interface shown in this chapter, or write your own interface. Either way, be sure to implement all of the common list operations.

public interface GeneralList <E>{ //methods to be implemented public void add(E element);//add element to the list public void add(int index, E element);//add element to a secific index public void clear();//clears the list public boolean contains(E element);//searchs for a specific eleement public E get(int index);//gets the element at a spacific position public int indexOf(E element);//gets the first occurrence of the specific element public boolean isEmpty();//determnes if te list is empty public boolean remove(E element);//removes an element from the list public E remove(int index); //removes the element at a specific index public E set(int index, E element);//replaces the elementata specific index public int size();//gets the size of the list }

Explanation / Answer

The code is given below step by step and also the explaination is given in the programmatical comments

public interface ArrayListOPERATIONS<T> extends Cloneable {
public boolean isEmpty(); //Method for determining whether the list is empty.
public boolean isFull(); //Method for determining whether the list is full.
public int listSize(); //Method for return the number of elements in the list.
public int maxListSize(); //Method for return the maximum size of the list.
public void print(); //Method for output the elements of the list.
public Object clone(); //Returns a copy of objects data in store. Clones only the references, not the objects
public boolean isItemAtEqual(int location, T item); //Method for determining whether item is the same as the item in the list at location.
public void add(int location, T insertItem); //Method for insert insertItem in the list at the position
public void insertEnd(T insertItem); //Method for insert insertItem at the end of the list.
public void removeAt(int location); //Method for remove the item from the list at location.
public T retrieveAt(int location); //Method for retrieve the element from the list at location.
public void replaceAt(int location, T repItem); //Method for replace the element in the list at location with repItem.
public void clearList(); //Method for remove all the elements from the list.
public int search(T searchItem); //Method for determining whether searchItem is in the list.
public void remove(T removeItem); //Method for remove an item from the list.
}
//Class: ArrayListClass implements
//Interface: ArrayListOPERATIONS
public abstract class ArrayListClass<T> implements ArrayListOPERATIONS<T>, Cloneable {
protected int length; //for store the length of the list
protected int maxSize; //for store the maximum size of the list
protected T[] list; //array for hold the list elements

//Default constructor
public ArrayListClass() {
maxSize = 100;
length = 0;
list = (T[]) new Object[maxSize];
}

//Alternate Constructor
public ArrayListClass(int size) {
if(size <= 0) {
System.err.println("The array size must be positive. Creating an array of size 100. ");
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = (T[]) new Object[maxSize];
}

public boolean isEmpty() {
return (length == 0);
}

public boolean isFull() {
return (length == maxSize);
}

public int listSize() {
return length;
}

public int maxListSize() {
return maxSize;
}

public void print() {
for (int i = 0; i < length; i++)
System.out.print(list[i] + " ");
System.out.println();
}

public Object clone() {
ArrayListClass<T> copy = null;
try {
copy = (ArrayListClass<T>) super.clone();
}
catch (CloneNotSupportedException e) {
return null;
}
copy.list = (T[]) list.clone();
return copy;
}

public boolean isItemAtEqual(int location, T item) {
if (location < 0 || location >= length) {
System.err.println("The location of the item for be compared is out of range.");
return false;
}
return (list[location].equals(item));
}

public void clearList() {
for (int i = 0; i < length; i++)
list[i] = null;
length = 0;
System.gc(); //invoke garbage collector
}

public void removeAt(int location) {
if (location < 0 || location >= length)
System.err.println("The location of the item for be removed is out of range.");
else {
for(int i = location; i < length - 1; i++)
list[i] = list[i + 1];
list[length - 1] = null;
length--;
}
}

public T retrieveAt(int location) {
if (location < 0 || location >= length) {
System.err.println("The location of the item for be retrieved is out of range.");
return null;
}
else
return list[location];
}

public abstract void add(int location, T insertItem);
public abstract void insertEnd(T insertItem);
public abstract void replaceAt(int location, T repItem);
public abstract int search(T searchItem);
public abstract void remove(T removeItem);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote