我有这段代码是IntLinkedList类
public class IntLinkedList {
private Node head;
public void addFirst(int data) {
head = new Node(data,head);
}
public Node copy(){
Node current = head; // used to iterate over original list
Node newList = null; // head of the new list
Node tail = null; // point to last node in new list
while (current != null)
{
// special case for the first new node
if (newList == null)
{
newList = new Node(current.data,null);
tail = newList;
}
else
{
tail.next = new Node(current.data,null);
tail = tail.next;
}
current = current.next;
}
return newList;
}
private class Node {
int data;
Node next;
Node(int data,Node next) {
this.data = data;
this.next = next;
}
}
}
我正在尝试使用以下JUnit代码测试复制方法
public class IntLinkedListTest {
/** Reference to linked list under test */
private IntLinkedList lst;
/** Creates a linked list for testing. */
@Before
public void setUp() {
lst = new IntLinkedList();
lst.addFirst(30);
lst.addFirst(10);
lst.addFirst(40);
lst.addFirst(20);
}
/** Tests copying a non-empty list. */
@Test
public void testCopy() {
IntLinkedList cpy = lst.copy();
assertEquals(lst.toString(),cpy.toString());
}
}
我想从Copy()方法获取IntLinkedList类返回的列表,并在JUnit中对其进行测试。我也尝试返回IntLinkedList和Object类型,但我不断收到诸如“类型不匹配:无法从IntLinkedList.Node转换为IntLinkedList”之类的错误。我对LinkedList的经验很少,但是对Java类,对象的引用却很有经验,但这对我来说是新领域。有人可以帮忙吗?