嘿,我正在研究一個程序,我遇到了一個奇怪的問題,希望是一個簡單的修復。所以我給了一個程序的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