In: Computer Science
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work.
Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO.
Test (Main)
List list = new SparseList<>(); list.add("0");
list.add("1");
list.add(4, "4");
will result in the following list of size 5: [0, 1, null, null, 4].
list.add(3, "Three");
will result in the following list of size 6: [0, 1, null, Three,
null, 4].
However,
list.set(3, "Three);
is going to produce a list of size 5 (unchanged): [0, 1, null,
Three, 4].
When removing an element from the list above, via list.remove(1)); the result should be the following list of size 4: [0, null , Three, 4]
Using the other constructor, List list = new SparseList<>(); would just result in all the null values produced by remove, get, set and toString be replaced with the specified value, e.g., [0, 1, null, null, 4]. Consider completing the implementation of your class with the default constructor only first, and then make the needed modifications.
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class SparseList implements List{
private int endIndex = 0;
private HashMap list;
public SparseList() {
list = new HashMap<>();
}
public SparseList(E[] arr) {
list = new HashMap<>();
for(int i = 0; i
list.put(i, arr[i]);
}
endIndex = arr.length - 1;
}
@Override
public boolean add(E e) {
endIndex++;
list.put(endIndex, e);
return true;
}
@Override
public void add(int index, E element) {
list.put(index, element);
}
@Override
public E remove(int index) {
return list.remove(index);
}
@Override
public E get(int index) {
return list.get(index);
}
@Override
public E set(int index, E element) {
E previous = list.get(index);
list.put(index, element);
return previous;
}
@Override
public int size() {
return endIndex + 1;
@Override
public void clear() {
list.clear();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public String toString() {
return list.toString();
}
}
Please try below code. If you still facing error requesting you provide the complete code. This would help us to identify and fix the problem in a faster manner.
-----------------------------------------------------------------------
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class SparseList implements List{
private int endIndex = 0;
private HashMap list;
public SparseList() {
this(10);
}
/*public SparseList(int lenOfArr) {
list = new HashMap<>();
for(int i = 0; i<=lenOfArr; i ++){
list.put(i, list[i]);
}
endIndex = arr.length - 1;
}*/
@Override
public boolean add(Object e) {
if (endIndex==0){
list.put(endIndex, e);
}
else {
endIndex++;
list.put(endIndex, e);
}
return true;
}
@Override
public void add(int index, Object element) {
list.put(index, element);
}
@Override
public Object remove(int index) {
return list.remove(index);
}
@Override
public Object get(int index) {
return list.get(index);
}
@Override
public Object set(int index, Object element) {
List previous = list.get(index);
list.put(index, element);
return previous;
}
@Override
public int size() {
return endIndex + 1;
}
@Override
public void clear() {
list.clear();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public String toString() {
return list.toString();
}
@Override
public List subList(int startIndex, int endIndex) {
// TODO
}
}