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

问题描述 投票:0回答:2

所以,我不太确定我的toString方法有什么问题。当我运行我的测试时,我只是一直出错,这是不正确的。

基本上我正在做的是实现循环DoublyLinkedList数据结构。与单链表一样,双链表中的节点具有对下一节点的引用,但与单链表不同,双链表中的节点也具有对前一节点的引用。此外,因为列表是“循环”的,所以列表中最后一个节点中的“下一个”引用指向列表中的第一个节点,列表中第一个节点中的“prev”引用指向最后一个节点。列表。

这是我的代码:

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()是不准确的。我为那些人提出了不同的问题,如果你想帮助我,我会非常感激。

java arraylist nodes tostring doubly-linked-list
2个回答
0
投票

我在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]
}

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.