如何使用Java中的节点编写toString方法

时间:2019-03-18 19:01:07

标签: java arraylist nodes tostring doubly-linked-list

因此,我不太确定我的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()不正确。我为那些人分别提出了问题,如果您想帮助我解决这些问题,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

我在您的add()方法和toString()方法中做了一些更改:

Grandparent.prototype.<what?>.call(this, ...)

这是要测试的主要内容:

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;
    }
}

答案 1 :(得分:0)

我在上面的代码中犯了一些小错误,所以我弄清楚了,我将发布答案。

@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;
    }
}