In: Computer Science
You will implement the MyList ADT according to the following:
1. MyList must implement the List interface. It will look something like below:
public class MyList<E> implements List<E> {
... } Note: As said in the class, when implementing an interface, you have to implement each method in it. There are a lot of methods in the List interface. We are going to address this issue below.
2. The initial capacity of MyList must be 2. This is not an ideal initial capacity. A smaller initial
capacity allows us to test out our code more quickly.
3. You must implement the following methods:
@SuppressWarnings("unchecked") public MyList() {
... } public int size() {
... } public boolean isEmpty() {
... } public boolean add(E e) {
... } public void add(int index, E element) {
... } public E get(int index) {
... } public E set(int index, E element) {
... } public E remove(int index) {
... } You can choose to implement any other methods as you need – private or public. For any method you don’t need but is required by the List interface, you can stub them out like below:
public void clear() {
StackTraceElement[] stackTrace =
new Throwable().getStackTrace(); String methodName = stackTrace[0].getMethodName(); throw new UnsupportedOperationException("Method '"
+ methodName + "' not implemented!"); }
4. There is at least one place where you will encounter an avoidable “unchecked” compiler warning. To get a clean compilation, use: @SuppressWarnings("unchecked").
But don’t abuse it for any other places where a warning is avoidable but due to your questionable coding skills.
Here is the completed code for this problem including a Test class for basic testing. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// MyList.java
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MyList<E> implements List<E> {
// underlying array
private E array[];
// number of elements
private int size;
// initial size
private static int INITIAL_CAPACITY = 2;
// constructor initializing MyList object using initial capacity
@SuppressWarnings("unchecked")
public MyList() {
array = (E[]) new Object[INITIAL_CAPACITY];
size = 0;
}
// helper method to resize the array when array is full
@SuppressWarnings("unchecked")
private void resize(int newSize) {
// creating array of new size
E newArr[] = (E[]) new Object[newSize];
// copying elements
for (int i = 0; i < size && i < newSize; i++) {
newArr[i] = array[i];
}
// replacing old with new array
array = newArr;
// updating size if the array is smaller than size (in case of
// downsizing (in future))
if (array.length < size) {
size = array.length;
}
}
@Override
public boolean add(E element) {
// resizing array to double capacity if full
if (size == array.length) {
resize(array.length * 2);
}
// adding to end
array[size] = element;
size++;
return true;
}
@Override
public void add(int index, E element) {
// throwing exception if index is invalid
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Invalid index");
}
// resizing array if necessary
if (size == array.length) {
resize(array.length * 2);
}
// shifting elements starting from index to one place right
for (int i = size; i > index; i--) {
array[i] = array[i - 1];
}
// adding element to index, and updating size
array[index] = element;
size++;
}
// returns a String containing list elements
public String toString() {
String data = "[";
for (int i = 0; i < size; i++) {
data += array[i];
if (i != size - 1) {
data += ", ";
}
}
data += "]";
return data;
}
@Override
public E get(int index) {
// throwing exception if index is invalid
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Invalid index");
}
return array[index];
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public E remove(int index) {
// throwing exception if index is invalid
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Invalid index");
}
// storing element to remove
E element = array[index];
// shifting all elements starting from index+1 position, to one place left
for (int i = index; i < size - 1; i++) {
array[i] = array[i + 1];
}
//updating size, returning removed element
size--;
return element;
}
@Override
public int size() {
return size;
}
@Override
public E set(int index, E element) {
// throwing exception if index is invalid
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Invalid index");
}
//storing old value
E oldValue = array[index];
//replacing with new value
array[index] = element;
//returned old value
return oldValue;
}
// below methods are stubbed out since not required
/**
* NOT implemented
*/
@Override
public boolean addAll(Collection<? extends E> arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public boolean addAll(int arg0, Collection<? extends E> arg1) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public void clear() {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public boolean contains(Object arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public boolean containsAll(Collection<?> arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public int indexOf(Object arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public Iterator<E> iterator() {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public int lastIndexOf(Object arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public ListIterator<E> listIterator() {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public ListIterator<E> listIterator(int arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public boolean remove(Object arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public boolean removeAll(Collection<?> arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public boolean retainAll(Collection<?> arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public List<E> subList(int arg0, int arg1) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public Object[] toArray() {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
/**
* NOT implemented
*/
@Override
public <T> T[] toArray(T[] arg0) {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
String methodName = stackTrace[0].getMethodName();
throw new UnsupportedOperationException("Method '" + methodName
+ "' not implemented!");
}
}
//Test.java
public class Test {
public static void main(String[] args) {
// creating a MyList object, adding some numbers and testing various
// methods
MyList<Integer> list = new MyList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
System.out.println(list);
System.out.println("Size: " + list.size());
list.add(5, 999);
System.out.println(list);
System.out.println("get(5): " + list.get(5));
System.out.println("remove(5): " + list.remove(5));
System.out.println(list);
System.out.println("set(0, 1234)");
list.set(0, 1234);
System.out.println(list);
}
}
/*OUTPUT*/
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Size: 10
[0, 1, 2, 3, 4, 999, 5, 6, 7, 8, 9]
get(5): 999
remove(5): 999
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
set(0, 1234)
[1234, 1, 2, 3, 4, 5, 6, 7, 8, 9]