2013-09-30 38 views
0

任何時候我嘗試打印一個MyList對象,我得到'User @'一些十六進制數字。 有人可以用打印功能或打印方式幫助我嗎?我聽說試圖覆蓋toString函數,但我似乎無法得到這個工作,我不知道這是否是正確的事情。如何打印MyList的對象?

public class MyList { 
    private ListElement head, tail; //Forward declaration 
    void add(Object value) { 
    if (tail != null) { 
     tail.next = new ListElement(value); 
     tail = tail.next; 
    } 
    else { 
     head = tail = new ListElement(value); 
    } 
    } 
    Object remove() 
    { 
    assert head != null; // don't remove on empty list 
    Object result = head.value; 
    head = head.next; 
    if (head == null) { //was that the last? 
     tail = null; 
    } 
    return result; 
    } 
    //Nested class needed only in the implementation of MyList 
    private class ListElement { 
    ListElement(Object value) {this.value = value;} 
    Object value; 
    ListElement next; //defaults to null as desired 
    } 
    public static void main(String[] args) { 
    myList anInstance = new myList(); 
    String someValue = "A list element"; 
    anInstance.add(someValue); 

    String anotherValue = "Another value"; 
    anInstance.add(anotherValue); 
    } 
} 

我試過的倍率去是這樣的:

@Override 
    public String toString() { 
     return String.format(this.head); 
    } 
} 
+0

你需要實現'toString()'。你知道字符串連接是如何工作的嗎? –

+0

向我們展示您用來打印列表的代碼?你可以實現toString()或只是打印出你想要的用戶的屬性,即user.firstname – rabs

回答

0

此代碼是未測試實施toString方法的。試試看。

+0

這個工程!謝謝。 –

+0

不客氣 –

1

默認toString()方法打印對象(你看到的是十六進制數)的內存地址。

如果你想要的東西不同,你需要:

@Override 
public String toString() 
{ 
    //do stuff to build a string that describes your object 
    //return that string you just built 
} 
+0

我知道這個部分,但內部的東西是我遇到的麻煩。有關如何完成這項工作的任何建議? –

+0

取決於。你想如何描述你的對象?什麼是你想打印出的對象中的給定數據集的文字字符串? – nhgrif

+0

我想我只想看看這個(假設代碼列表):[「列表元素」,「另一個值」] –

2

幽州:

我試過的倍率去是這樣的:

@Override 
    public String toString() { 
     return String.format(this.head); 
    } 
} 

這是一個開始,現在不是隻打印頭,而是使用while循環遍歷整個列表,並創建一個包含所有元素信息的新String。然後返回該字符串。

@Override 
    public String toString() { 
     ListElement tail = this.tail; 
     // or you might need to start at the head element depending on which way 
     // you will iterate. 

     String returnString = ""; 

     // use a while loop here to go through your list 
     // and add pertinent info from each element to the returnString 

     return returnString; 
    } 
} 

請注意,如果你想成爲超高效,你會使用StringBuilder做你的串聯,但是您的應用程序,這很可能是過殺害和沒有必要的。注意2:希望ListElement有一個toString()方法,如果是這樣,請在while循環中使用它來獲取每個元素的信息。


下一次迭代:

@Override 
    public String toString() { 

     String returnString = ""; 

     ListElement currentElement = this.tail; 

     while (currentElement != null) { 
     returnString += // *** get toString() info from currentElement 
     currentElement = // **** reset currentElement to next element 
     } 

     return returnString; 
    } 
} 
+1

我認爲這就是問題所在。我不認爲我可以遍歷這個列表,或者至少我不知道如何迭代這個列表。我可以通過陣列的很好,但我不認爲這會在這裏工作。我是否需要在類中創建一個迭代器方法? –

+0

@RichieMiz:你知道如何獲得'next'元素,對吧?直到你用完元素,直到'next == null'。另請參閱有關ListElement的'toString()'方法的說明。確保你給這個類提供了這個方法,並且你的MyList的'toString()'方法在while循環中調用每個ListElement的'toString()'來獲得重要的信息。 –

+0

這是否完成了工作:'@Override public String toString(){ ListElement = this.tail; String returnString =「」; while(this.tail.next!= null){ returnString = returnString + ListElement; ListElement = this.tail.next; } return returnString; }' –