Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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方法调用void方法_Java_List_Tostring - Fatal编程技术网

Java toString方法调用void方法

Java toString方法调用void方法,java,list,tostring,Java,List,Tostring,嘿,伙计们,我正在做一个程序,我遇到了一个奇怪的问题,希望这是一个容易解决的问题。我得到了一个程序的shell代码,这个程序创建并打印一个整数链接列表。它有print()方法来格式化打印输出。然后,我被要求创建一个toString方法,该方法返回一个包含列表打印值的字符串。这是我的密码: public void print() { System.out.println("--------------------"); System.out.print("List elements

嘿,伙计们,我正在做一个程序,我遇到了一个奇怪的问题,希望这是一个容易解决的问题。我得到了一个程序的shell代码,这个程序创建并打印一个整数链接列表。它有print()方法来格式化打印输出。然后,我被要求创建一个toString方法,该方法返回一个包含列表打印值的字符串。这是我的密码:

public void print()
{
    System.out.println("--------------------");
    System.out.print("List elements: ");
    IntNode temp = front;
    while (temp != null)
    {
        System.out.print(temp.val + " ");
        temp = temp.next;
    }
    System.out.println("\n-----------------------\n");
}

public String toString()
{
    String result;
    result += System.out.print(print());
    return result;
}

正如您所料,我从编译器那里得到一个错误,说这里不允许使用'void'类型

你在做一些奇怪的事情:

result+=System.out.print(print())

您的方法
print()
不返回值,只将所有内容打印到控制台。您可能需要修改
print
以返回字符串值


更好的是,您需要基于当前
print
方法中的代码创建一个适当的
toString()
方法,只需在
print
中使用
toString()
,您就可以将系统输出与字符串混合。这不是我个人写的,但似乎是你想要的

public String getAsString()
{
    String output = new String();
    output += "--------------------\n";
    output += "List elements: \n";
    IntNode temp = front;
    while (temp != null)
    {
        output += (temp.val + " \n");
        temp = temp.next;
    }
    output += "\n-----------------------\n");

    return output;
}

public String toString()
{
    return getAsString;
}

基本上,问题是您需要从
print
方法获得
字符串。您当前的设计是:

  • print
    :使用我想要/需要的不同
    字符串打印此类实例
  • toString
    :尝试在
    print
    中使用生成的
    字符串
可以注意到,这是一个设计问题。实现应该是另一种方式:
toString
方法生成对象引用的所需
String
形式,而
print
方法只打印
toString
的结果

因此,只需在
toString
方法中调整您的代码即可实现您想要/需要的:

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("--------------------");
    sb.append('\n');
    sb.append("List elements: ");
    while (temp != null) {
        sb.append(temp.val);
        sb.append(" ");
        temp = temp.next;
    }
    sb.append("\n-----------------------\n");
    return sb.toString();
}

public void print() {
    System.out.println(toString());
}
这是一条有问题的路线。不能将控制台打印分配给变量


相反,在整个程序中使用字符串变量,并将最终结果打印到控制台。

我认为您想要的是这样的。注意,我使用的是stringbuilder,它比许多StringCon猫都好

我根据你的代码做了很多假设。。。但你很可能正试图做这样的事情

public String print(){
    StringBuilder result = new StringBuilder();
    result.append("--------------------\n");
    result.append("List elements: ");
    IntNode temp = front;
    while (temp != null)
    {
        result.append(temp.val);
        result.append(" ");
        temp = temp.next;
    }
    result.append("\n-----------------------\n");
    return result.toString()
}

public String toString()
{
    return print();
}

我不知道如果有的话,
System.out.print
会返回什么,但我确信将其返回值附加到字符串不是您想要的方式

我会让
print()
方法使用
toString
来构建格式化字符串:

public String toString() {
    String output = "--------------------\n";
    output += "List elements: ";
    IntNode temp = front;
    while (temp != null) {
        output += temp.val + " ";
        temp = temp.next;
    }
    output += "\n-----------------------\n";
    return output;
}

public void print() {
    System.out.print(toString());
}
注意:您也可以
StringBuilder
toString
中构建字符串,这样效率会更高

    System.out.print(print())
此语句正在调用您在代码中定义的方法。代码中方法的返回类型为void,表示我不会返回任何内容,而
PrintStream
类下的print方法接受参数,该参数又可以解析为字符串


返回一些值,然后尝试打印

@joey.enfield如果你阅读代码,你会发现它非常明确,我想。。。是的。。这里有太多的问题…我希望您得到一个错误,因为您不能将字符串连接与void一起使用。你想在那里做什么?即使
print
返回一个字符串,它仍然会中断,因为
System.out.print()
也会返回void
+=
对于类型void是未定义的。OP已经知道这一点:正如您所料,我从编译器那里得到一个错误,说这里不允许使用
void
类型。那么我真的不明白问题是什么?如果OP已经知道这是他问题的原因,那么他肯定能解决它?这是一个设计问题,正如我在回答中所指出的。非常感谢各位,我认为实验室的措辞让我很反感。
    System.out.print(print())