问题
So, I'm not quite sure what is wrong with my toString method. I just keep having an error when I run my tests that it's incorrect.
Basically what I am doing is implementing a cyclic DoublyLinkedList data structure. Like a singly linked list, nodes in a doubly linked list have a reference to the next node, but unlike a singly linked list, nodes in a doubly linked list also have a reference to the previous node. Additionally, because the list is "cyclic", the "next" reference in the last node in the list points to the first node in the list, and the "prev" reference in the first node in the list points to the last node in the list.
Here is my code:
public class DoublyLinkedList<E>
{
private Node first;
private int size;
@SuppressWarnings("unchecked")
public void add(E value)
{
if (first == null)
{
first = new Node(value, null, null);
first.next = first;
first.prev = first;
}
else
{
first.prev.next = new Node(value, first, first.prev);
first.prev = first.prev.next;
}
size++;
}
private class Node<E>
{
private E data;
private Node next;
private Node prev;
public Node(E data, Node next, Node prev)
{
this.data = data;
this.next = next;
this.prev = prev;
}
}
@SuppressWarnings("unchecked")
public void add(int index, E value)
{
if (first.data == null)
{
throw new IndexOutOfBoundsException();
} else if (index == 0)
{
first = new Node(value, first.next, first.prev);
}
else
{
Node current = first;
for (int i = 0; i < index - 1; i++)
{
current = current.next;
}
current.next = new Node(value, current.next, current.prev);
}
}
@SuppressWarnings("unchecked")
public void remove(int index)
{
if (first.data == null)
{
throw new IndexOutOfBoundsException();
}
else if (index == 0)
{
first = first.next;
}
else
{
Node current = first.next;
for (int i = 0; i < index - 1; i++)
{
current = current.next;
}--size;
current.next = current.next.next;
}
}
public E get(int index)
{
if(index < 0)
{
throw new IndexOutOfBoundsException();
}
if(index > size)
{
throw new IndexOutOfBoundsException();
}
Node current = first;
for (int i = 0; i < index; i++)
{
current = current.next;
}
return (E) current.data;
}
@SuppressWarnings("unchecked")
public int indexOf(E value)
{
int index = 0;
Node current = first;
while (current != current.next)
{
if (current.data.equals(value))
{
return index;
}
index++;
current = current.next;
}
return index;
}
public boolean isEmpty()
{
if (size == 0)
{
return true;
}
else
{
return false;
}
}
public int size()
{
return size;
}
Here is my toString() method that apparently marks that it's not correct when I run my tests, but I don't know what's wrong with it.
What it should do is return a string representation of the list, starting with "[", followed by each element separated by a comma and a space, and ending with "]". The last element is not followed by a comma and a space. An empty list generates a string with no spaces, just "[]". This implementation should match that in ArrayList.
@SuppressWarnings("unchecked")
public String toString()
{
if (first.data == null)
{
return "[]";
}
else
{
Node current = first;
String result = "[" + current.data;
while (current.next != null)
{
result += current.data + ", ";
current = current.next;
}
result += "]";
return result;
}
}
}
I know my removeMethod() is inaccurate. I made separate questions for those, if you would like to help me with those, I would greatly appreciate it.
回答1:
I have change a little in your add() method and toString() method:
public void add(E value) {
if (first == null) {
first = new Node(value, null, null);
} else {
Node current = first;
while (current.next != null) {
current = current.next;
}
current.next = new Node(value, null, current);
}
size++;
}
public String toString()
{
if (first == null)
{
return "[]";
}
else
{
String result = "[" + first.data;
Node current = first.next;
while (current != null)
{
result += ", " +current.data ;
current = current.next;
}
result += "]";
return result;
}
}
And here is the main to test:
public static void main(String[] args) {
DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list.toString()); // [1, 2, 3]
}
回答2:
I made some minor errors in my code up above, so I figured it out, I'm going to post my answer.
@SuppressWarnings("unchecked")
public String toString()
{
if (isEmpty())
{
return "[]";
}
else
{
String result = "[" + first.data;
Node current = first.next;
for(int i = 0; i < size-1; i++)
{
result += ", " + current.data;
current = current.next;
}
result += "]";
return result;
}
}
来源:https://stackoverflow.com/questions/55228367/how-to-write-a-tostring-method-using-nodes-in-java