我被分配编写一个名为MyDeque的类,它应该像Java的Deque类一样工作。我的MyDeque类应该有一个getFirst方法和一个removeFirst方法以及其他方法,所有这些都是我写的。但是,如果在空的MyDeque上调用getFirst或removeFirst(其中没有任何元素),我应该抛出NoSuchElementException,我这样做。我的代码也应该实现我写的接口IDeque。
我的教授发布了一些“测试”代码,该代码应该使用我的MyDeque类型,如果我的解决方案正确,则打印所有trues。当我运行他的测试代码时,我得到了所有的真理,除了最后一个说我正在抛出错误的异常。有人可以解释我怎么能正确地抛出一个能满足他测试的NoSuchElementException吗?我似乎无法弄明白。
这是我的界面,IDeque:
/** An interface defining a linked structure that can be manipulated from both
* the beginning and the end.
* @author T K.
* @version 1
*/
public interface IDeque<E>{
public void addFirst(E e);
public void addLast(E e);
public E removeFirst();
public E getFirst();
public boolean isEmpty();
}
这是我的MyDeque课程:
import java.util.NoSuchElementException;
/** A MyDeque is a list of items that can be manipulated from
* both the front and the back. The run time of MyDeque is O(1).
*
* @author H
* @version 2/19/15
*
*
*/
public class MyDeque<E> implements IDeque<E>{
private DequeNode<E> first;
private DequeNode<E> last;
private int length;
/** Constructs a new instance of MyDeque with the first and last DequeNodes set to null
* and length set to zero.
*/
public MyDeque(){
first = null;
last = null;
length = 0;
}
/** Returns the size of MyDeque which is an integer representation of how
* many elements have been added to the instance of MyDeque.
* @return An integer representation of the size of MyDeque.
*
*/
public int size(){
return length;
}
/** Adds an element to the beginning of the MyDeque.
* @param e An element of the type that MyDeque is defined to hold.
*/
public void addFirst(E e){ //0(1) run time.
if(length == 0){
last = first = new DequeNode<E>(e, null);
} else {
first = new DequeNode<E>(e, first);
}
length += 1;
}
/** Adds an element to the end of the MyDeque.
* @param e An element of the type that MyDeque is defined to hold.
* The idea for this method was taken from example code provided by TK.
*/
public void addLast(E e){ //0(1) run time.
if(length == 0){
last = first = new DequeNode<E>(e, null);
} else {
last.next = new DequeNode<E>(e, null);
last = last.next;
}
length += 1;
}
/** Removes the first element of a MyDeque and returns the removed element.
* @return A generic element.
* @throws NoSuchElementException A NoSuchElement Exception is thrown if
* an attempt to remove an item from an empty MyDeque is made.
*/
public E removeFirst(){ //0(1) run time.
if(length == 0){
throw new NoSuchElementException();
} else {
DequeNode<E> temp = new DequeNode<E>(first.data, null);
first = first.next;
length-=1;
return temp.data;
}
}
/** Returns the first element of the MyDeque.
* @returns A generic element.
* @throws NoSuchElementException A NoSuchElement Exception is thrown if
* an attempt to identify an item from an empty MyDeque is made.
*/
public E getFirst(){ //0(1) run time.
if(length == 0){
throw new NoSuchElementException();
} else {
return first.data;
}
}
public boolean isEmpty(){ //0(1) run time.
if(length == 0){
return true;
}
return false;
}
/** Creates a string representation of the MyDeque.
* @returns A string of MyDeque.
* Taken from example code provided by TK.
*/
public String toString() {
if (length==0) {
return "[]";
} else {
String res = "[" + first.data;
DequeNode<E> cur = first.next;
while (cur != null) {
res = res + ", " + cur.data;
cur = cur.next;
}
return res + "]";
}
}
/** Used only for unit-testing methods in the MyDeque class.
*
*/
public static void main(String[] args){
MyDeque<String> d = new MyDeque<String>();
// test addLast()
d.addLast("hello");
d.addLast("world");
d.addLast("TEST1");
d.addLast("TEST2");
System.out.println(d.toString());
//test addFirst()
d.addFirst("AddFirst");
System.out.println(d.toString());
// test size()
System.out.println(d.size());
// test removeFirst()
System.out.println(d.removeFirst());
System.out.println(d.toString());
MyDeque<String> a = new MyDeque<String>();
//a.removeFirst();
// test getFirst()
String s = d.getFirst();
System.out.println(s);
// test isEmpty()
System.out.println(d.isEmpty());
}
class DequeNode<E>{
public E data;
public DequeNode<E> next;
public DequeNode(E data, DequeNode<E> next){
this.data = data;
this.next = next;
}
}
}
这是他的测试代码:
import java.util.*;
public class DequeTest {
public static void main(String[] args) {
MyDeque<String> d = new MyDeque<String>();
System.out.print(d.isEmpty());
System.out.println(" - isEmpty on initial list");
d.addLast("B");
System.out.print(!d.isEmpty());
System.out.println(" - isEmpty after one addition");
System.out.print(d.getFirst().equals("B"));
System.out.println(" - getFirst after one addLast");
d.addLast("C");
d.addFirst("A");
String s = d.removeFirst() + d.removeFirst() + d.removeFirst();
System.out.print(s.equals("ABC"));
System.out.println(" - Sequence of adds followed by removes");
System.out.print(d.isEmpty());
System.out.println(" - isEmpty after all removed");
try {
d.removeFirst();
}
catch (NoSuchElementException e) {
System.out.println("true - NoSuchElementException thrown");
}
catch (Exception e) {
System.out.println("false - Wrong exception type thrown");
}
d.addFirst("B");
d.addFirst("A");
d.addLast("C");
s = d.removeFirst() + d.removeFirst() + d.removeFirst();
System.out.print(s.equals("ABC"));
System.out.println(" - Sequence of adds followed by removes");
System.out.print(d.isEmpty());
System.out.println(" - isEmpty after all removed");
try {
d.getFirst();
}
catch (NoSuchElementException e) {
System.out.println("true - NoSuchElementException thrown");
}
catch (Exception e) {
System.out.println("false - Wrong exception type thrown");
}
}
}