Create a class called 'IndexedList' which encapsulates a [simplified] index-based list collection. See Figure 1 for UML.

Figure 1 - UML of assignment: see image.

Be sure to keep the following stipulations in mind:

  • This class MUST utilize an array-based data structure.
    • You very well could create a link-based indexed list class, though that's something you can do on your own time.
  • Class 'IndexedList' does NOT need to implement, nor overload, nor override ANY of the methods from 'ListADT' since they should be inherited from class 'Custom_ArrayList'.
    • If not done so already, students should ensure that class 'Custom_ArrayList' is completed BEFORE starting on this assignment.
  • All of the "add" methods must work the way as they did in class 'UnorderedArrayList'; which means that:
    • The "addAfter" method should throw an exception if the 'target' element is NOT found within the indexed list.
    • Each method must expand the array's capacity when appropriate to do.
  • Function "get" simply returns the element at the specified [parameter] index.
    • If the argument exceeds our list's bounds (depending on what our 'rear' index is), throw an [IndexOutOfBounds] exception.
  • Function "replace" should do TWO things:
    • Assign the element parameter into the 'list' array at the specified index. Like with function "get", if the provided index value exceeds our list's bounds, then throw an [IndexOutOfBounds] exception.
    • Return the [old] element that was replaced (see Figure 2).
  • Function "indexOf" should work similarly to how it does with strings.
    • Find the FIRST element (in the array) that matches the parameter and return its index location, otherwise return negative one if it's not found.
  • Function "subList" should be similar to class 'String's "substring" method.
    • It should create and return an array containing all the elements between the provided index parameters (including the first, yet excluding the last). Note: NONE of the elements should be removed from the collection.
    • Make sure to throw the following exceptions (near the beginning of this function): IllegalArgument - If the 'start' parameter's value is GREATER than the 'end' parameter's value. IndexOutOfBounds - If either parameter goes beyond the bounds of our list (given whatever 'rear's value is) Like we did in functions "get" and "replace".
    • Also make sure that the array's size is of perfect length. For example, if 'start' is 1 and 'end' is 4, then the length of the returned array should be 3 (which is large enough to hold all the elements from indices one through three. See Figure 3 for an example.

Test

public class Test
{
public static void main(String[] args)
{
IndexedList< String > letters = new IndexedList();
letters.addToRear("A");
letters.addToFront("B");
letters.addAfter("C", "B");
printList(letters);

String value = letters.replace(1, "D");
System.out.println("Value = " + value);
printList(letters);
}

public static void printList(IndexedList list) {...}
}

Figure 2 - Adding elements to a list, then replacing one of them with a new value: see image.

Test

public class Test
{
public static void main(String[] args)
{
IndexedList< String > letters = new IndexedList();
letters.addToRear("A");
letters.addToRear("B");
letters.addToRear("C");
letters.addToRear("D");
letters.addToRear("E");

Object values[] = letters.subList(1, 4);

for(Object X : values)
System.out.print(X + " ");
System.out.println();
}
}

Figure 3 - Testing method "subList": see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.