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

查看:30
本文介绍了如何使用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 数据结构.与单向链表一样,双向链表中的节点都有对下一个节点的引用,但与单向链表不同的是,双向链表中的节点也有对前一个节点的引用.另外,由于链表是循环"的,链表最后一个节点的next"引用指向链表的第一个节点,链表第一个节点的prev"引用指向链表的最后一个节点.列表.

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天全站免登陆