[英]How to write a toString method using nodes in java
因此,我不太確定我的toString方法有什么問題。 在運行測試時,我只是一直出錯,認為它是錯誤的。
基本上我正在做的是實現循環DoublyLinkedList數據結構。 像單鏈列表一樣,雙鏈列表中的節點都有對下一個節點的引用,但是與單鏈列表不同,雙鏈列表中的節點也有對前一個節點的引用。 此外,由於列表是“循環的”,因此列表中最后一個節點中的“下一個”引用指向列表中的第一個節點,列表中第一個節點中的“上一個”引用指向列表中的最后一個節點名單。
這是我的代碼:
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;
}
這是我的toString()方法,顯然在運行測試時標記為不正確,但我不知道它有什么問題。
它應該做的是返回列表的字符串表示形式,以“ [”開頭,后跟每個元素,以逗號和空格分隔,以“]”結尾。 最后一個元素后沒有逗號和空格。 一個空列表將生成一個無空格的字符串,只是“ []”。 此實現應與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;
}
}
}
我知道我的removeMethod()不准確。 我為那些人分別提出了問題,如果您想幫助我解決這些問題,我將不勝感激。
我在您的add()方法和toString()方法中做了一些更改:
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;
}
}
這是要測試的主要內容:
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]
}
我在上面的代碼中犯了一些小錯誤,所以我弄清楚了,我將發布答案。
@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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.