Question
INTRO JAVA This is my version of the class ArrayList, called ListArray. Please change it to a GENERIC CLASS. You will need to store an
INTRO JAVA
This is my version of the class ArrayList, called ListArray. Please change it to a GENERIC CLASS. You will need to store an array of Objects and cast to the generic type E when needed (such as when returning an element stored in the ArrayList). Here is my ArrayList (not generic): Please make all changes needed to make it compltely generic.
public class ListArray {
private String[] array;
private int numElements;
public ListArray() {
array = new String[10];
numElements = 0;
}
public ListArray(int size) {
array = new String[size];
numElements = 0;
}
/**
* Copy constructor for ListArray
* Creates a new list array of the
* same size as the ListArray passed
* in as a parameter, and copies the
* parameter's data into the new
* list array using a for loop
* Also sets the numElements to be
* the same as the parameter's
* numElements
* @param la the ListArray to copy
*/
public ListArray(ListArray la) {
array = new String[la.array.length];
for (int i = 0; i < la.size(); i++) {
array[i] = la.get(i);
}
numElements = la.size();
}
/**Accessor Methods*/
/**
* Returns whether the list array is
* currently empty
* @return whether the list array is
* empty
*/
public boolean isEmpty() {
for (int i = 0; i < array.length; i++) {
if (array[i] != null) {
return false;
}
}
return true;
}
/**
* Returns the current number of
* elements stored in the list
* array
* @return the number of elements
*/
public int size() {
/*
for (int i = 0; i < array.length - 1; i++) {
while (!array[i].equals("null")) {
numElements++;
}
}
*/
return numElements;
}
/**
* Returns the element at the specified
* index. Prints an error message and
* returns an empty String if index is
* larger than or equal to numElements
* @param index the index of the element
* to access
* @return the element at index
*/
public String get(int index) {
if (index >= numElements) {
System.out.println("Error: Cannot get element at index " + index + "." +
" Outside bounds of list array.");
System.out.println("Index: " + index + ", Size: " + numElements);
return null;
} else {
return array[index];
}
}
/**
* Uses the linearSearch algorithm to
* locate an element in the list array
* @param str the element to locate
* @return whether or not the element
* is in the list array
*/
public boolean contains(String str) {
for (int i = 0; i < numElements; i++) {
if (array[i].equals(str)) {
return true;
}
}
return false;
}
/**
* Uses the linearSearch algorithm to
* locate an element in the list array
* @param str the element to locate
* @return the location of the element
* or -1 if the element is not in the
* list array
*/
public int indexOf(String str) {
for (int i = 0; i < numElements; i++) {
if (array[i].equals(str)) {
return i;
}
}
return -1;
}
/**
* Determines whether the list array
* is currently full
* @return whether the list array
* is at maximum capacity
*/
private boolean atCapacity() {
boolean isFull = true;
for(int i = 0; i < array.length; i++) {
if(array[i] == null) {
isFull = false;
break;
}
}
return isFull;
}
/**Mutator Methods*/
/**
* Resizes the list array by making a new
* array that has a length (capacity)
* 10 larger than the current array's
* length (capacity)
*/
private void reSize() {//array2 never gets passed back to the main code
int newlength = array.length + 10;
String array2[] = new String[newlength];
for (int i = 0; i < array.length; i++) {
array2[i] = array[i];
}
array = array2;
}
/**
* Inserts a new element to the end
* of the list array.
* Resizes the list array if it is
* at capacity before inserting
* @param str the element to insert
*/
public void add(String str) {
if (atCapacity()) {
reSize();
}
array[numElements] = str;
numElements++;
}
/**
* Inserts a new element at the specified
* index in the list array.
* Resizes the list array if it is
* at capacity before inserting
* @param index the index at which to insert
* @param str the element to insert
*/
public void add(int index, String str) {
if (index > array.length - 1) {
System.out.println("Error: Cannot add element " + str + " at index " + index + ".");
System.out.println("Index is outside the bounds of the list array.");
System.out.println("Index: " + index + ", Size: " + size());
return;
}
if (atCapacity()) {
reSize();
array[index] = str;
}
for(int i = numElements; i > index; i--) {
array[i] = array[i - 1];
}
array[index] = str;
numElements++;
}
/**
* Assigns a new value to the list array
* at a specified index
* @param index the index at which to update
* @param str the new element
*/
public void set(int index, String str) {
array[index] = str;
}
/**
* Removes an element at a specified index in
* the list array
* @param index the index at which to remove
* @return the element that was removed
*/
public String remove(int index) {
if (index >= array.length) {
System.out.println("Error: Cannot remove element at index " + index + ". Outside bounds of list array.");
return "";
}
String removed = array[index];
for (int i = index; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
numElements--;
return removed;
}
/**
* 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(String str) {
boolean result = false;
int index = -1;
for (int i = 0; i < numElements; i++) {
if (array[i].equals(str)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("Error: Cannot remove element " + str + ". No such element in the list array.");
return result;
}
for (int i = index; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
result = true;
numElements--;
return result;
}
/**Additional Methods*/
/**
* Creates a String of all elements,
* with [] around the elements,
* each element separated from the next
* with a comma
*/
@Override public String toString() {
String formattedArr = "";
for(int i = 0; i < numElements; i++) {
formattedArr = formattedArr + array[i];
if (i + 1 < numElements) {
formattedArr = formattedArr + ", ";
}
}
return "[" + formattedArr + "]";
}
}
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