Question
The implementation of several methods is (or can be) the same between our ArrayList and LinkedList. Write a common abstract superclass called AbstractList that implements
The implementation of several methods is (or can be) the same between our ArrayList and LinkedList. Write a common abstract superclass called AbstractList that implements the common behavior and is extended by both ArrayList and LinkedList. Factor out the common code from the two list classes into this abstract superclass so that no code duplication occurs between the two. Use iterators wherever possible in the abstract code to ensure that the implementation is efficient for both types of lists.
(Just want you to create an abstract class of
public E get(int index);
and
public int indexOf(E value);
Below you will first find the public E get(int index) { of ArrayList.java then public int indexOf(E value) { of ArrayList.java then public E get(int index) { of LinkedList.java and then public int indexOf(E value) { of LinkedList.java. I also have added comments on top of the code so you do not have any problems figuring out which code is which one.
IF YOU DON'T KNOW HOW TO DO IT, THEN PLEASE LEAVE IT FOR SOMEONE WHO KNOWS HOW TO DO IT PROPERLY.
import java.util.*;
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not) // post: returns the value at the given index in the list //ArrayList.java
public E get(int index) { checkIndex(index); return elementData[index]; // post: creates a comma-separated, bracketed version of the list public String toString() { if (size == 0) { return "[]"; } else { String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } result += "]"; return result; } } // post : returns the position of the first occurrence of the given // value (-1 if not found) //ArrayList.java
public int indexOf(E value) { for (int i = 0; i < size; i++) { if (elementData[i].equals(value)) { return i; } } return -1; }
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not) // post: returns the value at the given index in the list //LinkedList.java
public E get(int index) { checkIndex(index); ListNode current = nodeAt(index); return current.data; }
// post: creates a comma-separated, bracketed version of the list public String toString() { if (size == 0) { return "[]"; } else { String result = "[" + front.next.data; ListNode current = front.next.next; while (current != back) { result += ", " + current.data; current = current.next; } result += "]"; return result; } }
// post : returns the position of the first occurrence of the given // value (-1 if not found) //LinkedList.java
public int indexOf(E value) { int index = 0; ListNode current = front.next; while (current != back) { if (current.data.equals(value)) { return index; } index++; current = current.next; } return -1; }
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