Я пытаюсь создать индивидуальный LinkedList, чтобы лучше понять структуру данных. Я не мог понять, в чем проблема моего класса LinkedList.Почему мой настраиваемый LinkedList не работает?
package numberlist.primitivelist.objectlist;
public class ObjectLinkedList extends ObjectList implements Copiable {
Node firstNode;
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: ObjectLinkedList() description:constructor
*
* @author Jinyu Wu Date: 2017/2/4
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public ObjectLinkedList() {
firstNode = null;
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: add() description: Insert an item into the list
*
* @param index position of the list
* @param obj the element is going to be inserted
* @author Jinyu Wu Date: 2017/2/4
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public void add(int index, Object obj) {
Node tempNode = firstNode;
Node currentNode = new Node(obj);
if (index == 0) {
firstNode = currentNode;
return;
}
if (index < 0 || index > size()) {
System.out.println("add(ObjectLinkedList) index out of bound exception");
} else {
for (int i = 1; i <= index; i++) {
tempNode = tempNode.getNext();
if (i == index - 1) {
if (index != size() - 1) {
currentNode.setNext(tempNode.getNext());
} else {
currentNode.setNext(null);
}
tempNode.setNext(currentNode);
}
}
}
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: removeAt() description: remove an item from a position of the
* list
*
* @param index position in the list
* @author Jinyu Wu Date: 2017/2/4
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public void removeAt(int index) {
if (index < 0 || index > size()) {
System.out.println("removeAt(ObjectLinkedList) index out of bound exception");
} else {
Node tempNode = firstNode;
if (index == 0) {
firstNode = firstNode.getNext();
} else {
for (int i = 1; i <= index; i++) {
tempNode = tempNode.getNext();
if (i == index - 1) {
if (index != size() - 1) {
tempNode.setNext(tempNode.getNext().getNext());
} else {
tempNode.setNext(null);
}
}
}
}
}
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: remove() description: remove a specific item from a position of
* the list
*
* @param obj target object is going to be removed
* @author Jinyu Wu Date: 2017/2/4
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public void remove(Object obj) {
if (size() > 0) {
Node tempNode = firstNode;
for (int i = 0; i <= size(); i++) {
if (tempNode.equals(obj)) {
tempNode.setNext(tempNode.getNext().getNext());
break;
}
if (i < size() - 1) {
tempNode = tempNode.getNext();
}
}
System.out.println("target object is not found inside the linkedList(remove)");
}
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: get() description:get an item from the list
*
* @param index position in the list
* @author Jinyu Wu Date: 2017/2/4
* @return double ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public Object get(int index) {
if (index < 0 || index > size()) {
System.out.println("get(ObjectLinkedList) index out of bound exception");
return null;
} else if (index == 0) {
return firstNode;
} else {
Node tempNode = firstNode;
for (int i = 0; i <= index; i++) {
if (i == index - 1) {
tempNode = tempNode.getNext();
return tempNode;
}
}
System.out.println("objectLinkedList get method nothing found");
return null;
}
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: toString() description: print out the content of the list
*
* @author Jinyu Wu Date: 2017/2/4
* @return Integer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public String toString() {
return "ObjectLinkedList{" + "firstNode=" + firstNode + '}';
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: find() description:get an item from the list
*
* @author Jinyu Wu Date: 2017/2/4
* @param obj Object is going to be found
* @return Integer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public int find(Object obj) {
Node tempNode = firstNode;
Node newNode = new Node(obj);
if (newNode.equals(firstNode)) {
return 0;
} else {
for (int i = 1; i < size(); i++) {
if (tempNode.equals(newNode)) {
return i;
}
tempNode = tempNode.getNext();
}
return -1;
}
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: size() description:get the size of the list
*
* @author Jinyu Wu Date: 2017/2/4
* @return Integer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public int size() {
int size = 1;
if (firstNode == null) {
return 0;
}
try {
for (Node n = firstNode; n.getNext() != null; n = n.getNext()) {
size++;
}
return size;
} catch (NullPointerException e) {
return size;
}
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: deepCopy() description: make a deepCoy for this object
*
* @author Jinyu Wu Date: 2017/2/4
* @return String ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public ObjectLinkedList deepCopy() {
ObjectLinkedList newList = new ObjectLinkedList();
Node currentNode = firstNode;
for (int i = 0; i < size(); i++) {
Node newNode = new Node(currentNode.getValue());
newList.add(i, newNode);
currentNode = currentNode.getNext();
}
return newList;
}
}
Вот что делают проверить это с помощью теста Junit
package numberlist.primitivelist.objectlist;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class ObjectLinkedListTest {
ObjectLinkedList list;
Money m1, m2;
Node node1, node2;
public ObjectLinkedListTest() {
}
@Before
public void setUp() {
list = new ObjectLinkedList();
m1 = new Money(5, (byte) 6);
node1 = new Node(m1);
list.add(0, node1);
m2 = new Money(2, (byte) 4);
node2 = new Node(m2);
list.add(1, node2);
}
/**
* Test of add method, of class ObjectLinkedList.
*/
@Test
public void testAdd() {
assertEquals(list.get(0), node1);
}
/**
* Test of removeAt method, of class ObjectLinkedList.
*/
@Test
public void testRemoveAt() {
list.removeAt(1);
assertNull(list.get(1));
}
/**
* Test of remove method, of class ObjectLinkedList.
*/
@Test
public void testRemove() {
list.remove(m2);
assertNull(list.get(1));
}
/**
* Test of get method, of class ObjectLinkedList.
*/
@Test
public void testGet() {
}
/**
* Test of toString method, of class ObjectLinkedList.
*/
@Test
public void testToString() {
}
/**
* Test of find method, of class ObjectLinkedList.
*/
@Test
public void testFind() {
assertEquals(list.find(m1), 0);
assertEquals(list.find(m2), 1);
}
/**
* Test of size method, of class ObjectLinkedList.
*/
@Test
public void testSize() {
assertEquals(list.size(), 2);
}
/**
* Test of deepCopy method, of class ObjectLinkedList.
*/
@Test
public void testDeepCopy() {
}
}
Здесь ошибка я получил:
Мой класс Node:
package numberlist.primitivelist.objectlist;
public class Node {
private Node nextNode;
private Object obj;
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: Node() description: constructor
*
* @author Jinyu Wu Date: 2017/2/4
* @param obj set the value
*/
public Node(Object obj) {
this.obj = obj;
this.nextNode = null;
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: getValue() description: get the value of object
*
* @author Jinyu Wu Date: 2017/2/4
* @return return the object
*/
public Object getValue() {
return this.obj;
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: setValue() description: setValue for the Node
*
* @author Jinyu Wu Date: 2017/2/4
* @param obj return the value
*/
public void setValue(Object obj) {
this.obj = obj;
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: getValue() description: get the next value of the currentNode
*
* @author Jinyu Wu Date: 2017/2/4
* @return return next node
*/
public Node getNext() {
if (nextNode != null) {
return this.nextNode;
} else {
return null;
}
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Method: setNext() description: set next value for the Node
*
* @author Jinyu Wu Date: 2017/2/4
* @param node set next node
*/
public void setNext(Node node) {
this.nextNode = node;
}
}
Как определяется класс 'Node'? – yeputons
Также, как вы пытались отладить код? Каковы ваши выводы, можете ли вы предоставить меньшую часть кода, которая по-прежнему ведет себя странно? – yeputons