******IN JAVA********
I need the following interface implemented accordingly.It is a linked list. The interface can be found below:
List.java
public interface List extends Iterable { /** * Insert an element at a specified location. * @param index * @param obj * @throws IndexOutOfBoundsException */ public void add(int index, T obj); /** * Append an object to the end of the list. * @param obj */ public boolean add(T obj); public void clear(); public boolean contains(T obj); /** * If obj is in the list, return the * index of the first occurrence. * Otherwise, return -1. * @param obj * @return */ public int indexOf(T obj); public boolean isEmpty(); public int lastIndexOf(T obj); /** * Get and return the value stored at the index. * * @param index * @return * @throws IndexOutOfBoundsException */ public T get(int index); public T remove(int index); public boolean remove(T obj); /** * Update the value in the list at the specified index. * Return the old value * @throws IndexOutOfBoundsException * @param index * @param obj * @return */ public T set(int index, T obj); public int size(); public Object[] toArray();}
I also need the following driver modified so that itworks with the implementation:
ListDriver.java
import java.util.Iterator;public class ListDriver { /** * @param args the command line arguments */ public static void main(String[] args) { int i = 0; List names = new AList<>(5); names.add(\"Alice\"); names.add(\"Bob\"); names.add(\"Carol\"); names.add(1, \"Eve\"); names.add(\"Eve\"); for (String name : names) System.out.println((i++) + \":\" + name); i=0; System.out.println(\"Size (should be 5): \" + names.size()); System.out.println(\"IndexOf(Eve) (should be 1): \" + names.indexOf(\"Eve\")); System.out.println(\"LastIndexOf(Eve) (should be 4): \" + names.lastIndexOf(\"Eve\")); System.out.println(\"Remove Eve (should be true):\" + names.remove(\"Eve\")); System.out.println(\"Size (should be 4): \" + names.size()); System.out.println(\"IndexOf(Eve) (should be 3): \" + names.indexOf(\"Eve\")); System.out.println(\"LastIndexOf(Eve) (should be 3): \" + names.lastIndexOf(\"Eve\")); for (String name : names) System.out.println((i++) + \":\" + name); i=0; System.out.println(\"Remove Eve (should be true):\" + names.remove(\"Eve\")); System.out.println(\"Size (should be 3): \" + names.size()); System.out.println(\"IndexOf(Eve) (should be -1): \" + names.indexOf(\"Eve\")); System.out.println(\"LastIndexOf(Eve) (should be -1): \" + names.lastIndexOf(\"Eve\")); System.out.println(\"Size (should be 3): \" + names.size()); System.out.println(\"Remove 0 (should be Alice): \" + names.remove(0)); System.out.println(\"Size (should be 2): \" + names.size()); names.add(0, \"Alice\"); names.add(1, \"Eve\"); names.add(\"Eve\"); names.add(1, \"Eve\"); names.add(\"Eve\"); names.add(1, \"Eve\"); names.add(names.indexOf(\"Carol\"), \"Eve\"); names.add(0, \"Eve\"); names.add(\"Eve\"); System.out.println(\"Size: \" + names.size()); for (String name : names) System.out.println((i++) + \":\" + name); i=0; System.out.println(\"Remove all instances of Eve using iterator... \"); Iterator it = names.iterator(); while(it.hasNext()) { if (it.next().equals(\"Eve\")) it.remove(); } for (String name : names) System.out.println((i++) + \":\" + name); i=0; System.out.println(\"Testing clear\"); names.clear(); System.out.println(\"Size (should be 0): \" + names.size()); } }
Thanks in advance. Will upvote!