如何使用Java中的节点编写toString方法 [英] How to write a toString method using nodes in java

查看:62
本文介绍了如何使用Java中的节点编写toString方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我不太确定我的toString方法出了什么问题.在运行测试时,我只是一直出错,认为它是不正确的.

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.

基本上我正在做的是实现循环DoublyLinkedList数据结构.像单链列表一样,双链列表中的节点都有对下一个节点的引用,但是与单链列表不同,双链列表中的节点也有对前一个节点的引用.此外,由于列表是循环的",因此列表中最后一个节点中的下一个"引用指向列表中的第一个节点,列表中第一个节点中的上一个"引用指向列表中的最后一个节点列表.

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.

这是我的代码:

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()方法,显然在我运行测试时标记为不正确,但我不知道它有什么问题.

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.

它应该做的是返回列表的字符串表示形式,以"["开头,然后是每个元素,中间用逗号和空格隔开,以]"结尾.最后一个元素后没有逗号和空格.一个空列表将生成一个无空格的字符串,只是"[]".此实现应与ArrayList中的实现匹配.

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

我知道我的removeMethod()不正确.我为那些人分别提出了问题,如果您想帮助我解决这些问题,我将不胜感激.

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.

推荐答案

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

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

这篇关于如何使用Java中的节点编写toString方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆