Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有递归Java的toString()_Java_String_Recursion - Fatal编程技术网

带有递归Java的toString()

带有递归Java的toString(),java,string,recursion,Java,String,Recursion,我的程序和提供的测试类有一个非常奇怪的问题。目标是以提供的精确格式返回字符串。然而,当我运行测试类时,它告诉我我的字符串是错误的,即使它与测试类中的字符串完全相同。我不知道为什么会出现一个不正确的字符串,即使该字符串是正确的。谢谢 这是一节课: public class RecursionLinkedList { private Node firstNode; private int numberOfEntries; public RecursionLinkedList() {

我的程序和提供的测试类有一个非常奇怪的问题。目标是以提供的精确格式返回字符串。然而,当我运行测试类时,它告诉我我的字符串是错误的,即使它与测试类中的字符串完全相同。我不知道为什么会出现一个不正确的字符串,即使该字符串是正确的。谢谢 这是一节课:

public class RecursionLinkedList
{
private Node firstNode;
private int numberOfEntries;

public RecursionLinkedList()
{
        firstNode = null;
        numberOfEntries = 0;
}

public void add(int aData)
{
        if(numberOfEntries == 0)
        {
    firstNode = new Node(aData);
        }
        else
        {
            firstNode = new Node(aData, firstNode);
        }

    numberOfEntries++;
}

/**
 * boolean contains(int aData)
 * 
 * See whether this RecursionLinkedList contains aData
 * @param aData  a data to be located
 * @return true if this RecursionLinkedList contains aData,
 *         or false otherwise.
 */
public boolean contains(int aData)
{
        Node currentNode = firstNode;
        if(currentNode == null) {
    return false;
        }

        if(currentNode.data == aData) {
    return true;
        }

        else {
    return contains1(aData, currentNode);
        }
}

/**
 * int getFrequencyOf(int aData)
 * 
 * Counts the number of times a given data appears in this
 * RecursionLinkedList.
 * 
 * @param aData  the data to be counted
 * @return the number of times aData appears in this RecursionLinkedList
 */
public int getFrequencyOf(int aData)
{
        Node currentNode = firstNode;
        int frequency = 0;
        if(currentNode == null) {
    return frequency;
        }

        else {
            frequency = getFrequencyOf1(aData, currentNode);
    return frequency;
        }

}

/**
 * String toString()
 * 
 * Return a string representation of this RecursionLinkedList. For example,
 * if this RecursionLinkedList contains 1, 2, 3, 5, 2 and 3 from the first
 * index to the last index, the returned string should be
 * "[1,2,3,5,2,3]"
 * @return the string representation of this RecursionLinkedList.
 */
public String toString()
{
        Node currentNode = firstNode;
        String str1 = "[";
        String str = toString1(currentNode);
        String str2 = "]";
        return str1 + str + str2;   
}

/**
 * int getIndexOf(int aData)
 * 
 * Return the index of the first aData where the first index of
 * the first item in this RecursionLinkedList is 0.
 * 
 * @param aData  the data to be located
 * @return the index of the first aData.
 */
public int getIndexOf(int aData)
{
        Node currentNode = firstNode;
        int index = 0;
        if(currentNode == null) {
            return 0;
        }

        else {
            index = getIndexOf1(aData, currentNode);
            return index;
        }

}

    public boolean contains1(int aData, Node node)
    {
        Node currentNode = node;

        if(currentNode == null) {
            return false;
        }

        if(currentNode.data == aData) {
            return true;
        } 

        else {
            return contains1(aData, currentNode.next);
        }
 }

    public int getFrequencyOf1(int aData, Node node)
{
    Node currentNode = node;
            int frequency = 0;
    if(currentNode == null) {
                return frequency;
    }

    if(currentNode.data == aData) {
                frequency = 1 + getFrequencyOf1(aData, currentNode.next);
    }

    else {
                frequency = getFrequencyOf1(aData, currentNode.next);
    }
            return frequency;
}

    public String toString1(Node node)
{
    Node currentNode = node;

            if(currentNode == null) {
                return "\b";
            }

            else {
                int i = currentNode.data;
                String str = Integer.toString(i);
                String str1 = str + ","+toString1(currentNode.next);
                return str1;
            }

}

    public int getIndexOf1(int aData, Node node)
{
    Node currentNode = node;
    int index = 0;
    if(currentNode == null) {
                return 0;
    }

    if(currentNode.data == aData) {
                return index;
    }

    else {
                index = 1 + getIndexOf1(aData, currentNode.next);
                return index;
    }

}

private class Node
{
    private int data;
    private Node next;

    private Node(int aData, Node nextNode)
    {
        data = aData;
        next = nextNode;
    }

    private Node(int aData)
    {
        this(aData, null);
    }
}
}
这是一个测试类:

import java.util.Random;


public class RecursionLLTester
{
public static void main(String[] args)
{
    RecursionLinkedList rll = new RecursionLinkedList();
    int point = 0;
    boolean isError = false;

    System.out.println("Create an empty RecursionLinkedList named rll.");

    System.out.print("Test the method contains() on an empty RecursionLinkedList: ");
    if(rll.contains(5) != false)
    {
        System.out.println("FAIL");
        System.out.println("Nothing is added into RecursionLinkedList rll.");
        System.out.println("rll.contains(5) should return 0.");
        System.out.println("But your rll.contains(5) returns " + rll.contains(5) + ".");
    }
    else
    {
        System.out.println("PASS");
        point++;
    }

    System.out.print("Test the method getFrequencyOf() on an empty RecursionLinkedList: ");
    if(rll.getFrequencyOf(5) != 0)
    {
        System.out.println("FAIL");
        System.out.println("Nothing is added into RecursionLinkedList rll.");
        System.out.println("rll.getFrequencyOf(5) should return 0.");
        System.out.println("But your rll.getFrequencyOf(5) returns " + rll.getFrequencyOf(5) + ".");
    }
    else
    {
        System.out.println("PASS");
        point++;
    }

    Random rand = new Random();
    int num = 20;
    int[] array = new int[5];
    for(int i = 0; i < 5; i++)
    {
        array[i] = 0;
    }

    System.out.println("Add the following integer into rll:");
    String s = "]";

    for(int i = 0; i < num  - 1; i++)
    {
        int temp = rand.nextInt(5);
        System.out.print(temp + " ");
        s = "," + temp + s;
        array[temp]++;
        rll.add(temp);
    }
    int temp = rand.nextInt(5);
    System.out.println(temp);
    s = "[" + temp + s;
    array[temp]++;
    rll.add(temp);


    System.out.println("\nTest the method contains() on a non-empty RecursionLinkedList");
    System.out.print("  - Test rll.contains(-1): ");
    if(rll.contains(-1) != false)
    {
        System.out.println("FAIL");
        System.out.println("No -1 has been added into the RecursionLinkedList rll.");
        System.out.println("rll.contains(-1) should return 0.");
        System.out.println("But your rll.contains(-1) returns " + rll.contains(-1));
    }
    else
    {
        System.out.println("PASS");
        point++;
    }

    for(int i = 0; i < 5; i++)
    {
        System.out.print("  - Test rll.contains(" + i + "): ");
        if(rll.contains(i) != (array[i] != 0))
        {
            System.out.println("FAIL");
            System.out.println("There are " + array[i] + " in RecursiveLinkedList rll.");
            System.out.println("rll.contains(" + i + ") should return " + (array[i] != 0));
            System.out.println("But your rll.contains(" + i + ") returns " + rll.contains(i));
            isError = true;
        }
        else
        {
            System.out.println("PASS");
        }
    }

    if(!isError)
    {
        point++;
        isError = false;
    }

    System.out.print("  - Test rll.contains(5): ");
    if(rll.contains(5) != false)
    {
        System.out.println("FAIL");
        System.out.println("No 5 has been added into the RecursionLinkedList rll.");
        System.out.println("rll.contains(5) should return 0.");
        System.out.println("But your rll.contains(5) returns " + rll.contains(5));
    }
    else
    {
        System.out.println("PASS");
        point++;
    }

    System.out.println("Test the method getFrequencyOf() on a non-empty RecursionLinkedList");
    System.out.print("  - Test rll.getFrequencyOf(-1): ");
    if(rll.getFrequencyOf(-1) != 0)
    {
        System.out.println("FAIL");
        System.out.println("No -1 has been added into the RecursionLinkedList rll.");
        System.out.println("rll.getFrequencyOf(-1) should return 0.");
        System.out.println("But your rll.getFrequencyOf(-1) returns " + rll.getFrequencyOf(-1));
    }
    else
    {
        System.out.println("PASS");
        point++;
    }

    for(int i = 0; i < 5; i++)
    {
        System.out.print("  - Test rll.getFrequencyOf(" + i + "): ");
        if(rll.getFrequencyOf(i) != array[i])
        {
            System.out.println("FAIL");
            System.out.println(i + " has been added to the RecursionLinkedList " + array[i] + " times.");
            System.out.println("rll.getFrequencyOf(" + i + ") should return " + array[i] + ".");
            System.out.println("But your rll.getFrequencyOf(" + i + ") returns " + rll.getFrequencyOf(i));
            isError = true;
        }
        else
        {
            System.out.println("PASS");
        }
    }

    if(!isError)
    {
        point++;
        isError = false;
    }

    System.out.print("  - Test rll.getFrequencyOf(5): ");
    if(rll.getFrequencyOf(5) != 0)
    {
        System.out.println("FAIL");
        System.out.println("No 5 has been added into the RecursionLinkedList rll.");
        System.out.println("rll.getFrequencyOf(5) should return 0.");
        System.out.println("But your rll.getFrequencyOf(5) returns " + rll.getFrequencyOf(5));
    }
    else
    {
        System.out.println("PASS");
        point++;
    }

    System.out.print("Test the method toString(): ");
    if(!s.equals(rll.toString()))
    {
        System.out.println("FAIL");
        System.out.println("rll.toString() should return the string \"" + s + "\".");
        System.out.println("But your rll.toString() returns the string \"" + rll.toString() + "\".");
    }
    else
    {
        System.out.println("PASS");
        point++;
    }


    System.out.println("Test the method getIndexOf()");
    System.out.println("Currently the rll is " + rll + ".");

    String[] str = rll.toString().split(",");
    str[0] = str[0].substring(1);
    str[str.length - 1] = str[str.length - 1].substring(0, 1);

    for(int i = -1; i <= 5; i++)
    {
        System.out.print("Test the method getIndexOf(" + i + "): ");
        if(getIndex(str,i) != rll.getIndexOf(i))
        {
            System.out.println("FAIL");
            System.out.println("The index of " + i + " should be " + getIndex(str,i) + ".");
            System.out.println("But your rll.getIndexOf(" + i + ") returns " + rll.getIndexOf(i) + ".");
            isError = true;
        }
        else
        {
            System.out.println("PASS");
        }
    }

    if(!isError)
    {
        point++;
        isError = false;
    }

    System.out.println("Your current point is " + point + ".");
}

public static int getIndex(String[] str, int s)
{
    int result = -1;

    for(int i = 0; i < str.length; i++)
    {
        if(s == Integer.parseInt(str[i]))
        {
            return i;
        }
    }

    return result;
}
}
问题出在您的toString节点方法中

\b字符是您在测试输出的末尾看到的字符

rll.toString应返回字符串[1,1,1,3,4,2,2,1,0,1,2,1,0,4,4,4,4,2,1,1,2]

但是您的rll.toString返回字符串[1,1,1,3,4,2,2,1,0,1,2,1,0,4,4,4,2,1,1,2,?]

对方法稍作调整,如下所示,测试通过

public String toString1(Node node)
{
    Node currentNode = node;
    if (currentNode == null) {
        return "\b";
    }
    int i = currentNode.data;
    String str = Integer.toString(i);
    if (currentNode.next != null) {
        str = str
            + "," + toString1(currentNode.next);
    } 
    return str;
}
尽管我建议您在这里做一些更改:

使用StringBuilder,而不是字符串连接时的所有抖动。 用循环替换递归。如果列表太大,那么使用递归方法将耗尽堆栈空间。 我认为您永远都不想返回那个\b字符,因为它不代表整数,而且您的indexOf方法似乎也被这个概念阻塞了。
它到底在哪里失败?你在期待什么?您得到了什么?我希望字符串与测试类中生成的字符串完全相同。字符串的格式为[5,4,3,2,1]。我的toString方法生成的字符串完全相同,但是if语句if!s、 测试类中出现equalsrll.toString是出于某种原因,即使此语句根本不是真的。您是否使用了调试器或其他工具来查看s值是什么以及rl1.toString值是什么?如果是,两个输出是什么?我运行了几次调试器,结果都是一样的。例如,我从测试课上得到[0,3,2,4,4,2,3,1,1,4,4,1,1,1,1,3,2,0,2,4,2,2],从我的toString方法中得到[0,3,2,4,4,2,3,1,1,4,4,1,1,4,4,4,4,4,4,1,4,4,4,4,4,1,4,4,4,4,4,4,2,4,4,1,1,1,1,1,1,1,1,1,1,1,1,4,@zackster1171:注意\b不会删除字符串中的最后一个字符;它只在打印时做一些特殊的事情。
public String toString1(Node node)
{
    Node currentNode = node;
    if (currentNode == null) {
        return "\b";
    }
    int i = currentNode.data;
    String str = Integer.toString(i);
    if (currentNode.next != null) {
        str = str
            + "," + toString1(currentNode.next);
    } 
    return str;
}