嘿,我正在研究一个程序,我遇到了一个奇怪的问题,希望是一个简单的修复。所以我给了一个程序的shell代码来创建和打印一个整数链接列表。它有print()方法来格式化打印输出。然后我被要求创建一个toString方法,该方法返回一个包含列表打印值的String。这是我的代码:toString方法调用void方法
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'类型在这里是不允许的。
@ joey.enfield如果你读的代码,你会发现它很明确的,我想。 –
..我做了..这里有这么多问题... –
我希望你得到一个错误,因为你不能使用字符串连接与void。你想在那里做什么? – azurefrog