2011-11-23 79 views
2

所以我寫的迭代器簡短的演示和鏈表:打印出一個LinkedList

import java.awt.List; 
    import java.util.ArrayList; 
    import java.util.Iterator; 
    import java.util.LinkedList; 


    class Marriage 
{ 
    String person1; 
    String person2; 

    Marriage(String p1, String p2) 
    { 
     person1 = p1; 
     person2 = p2; 
    } 
} 

public class MyArrayList { 
    Object[] container; 
    int currSize; 
    int numElements=0; 

    public MyArrayList(int initialSize) 
    { 
     container = new Object[initialSize]; 
     currSize = initialSize; 
    } 
    public MyArrayList() 
    { 
     this(10); 
    } 
    public int size() 
    { 
     return numElements; 
    } 

    public void add(Object ob) 
    { 
     if (numElements >= currSize) 
      resize(); 
     container[numElements++] = ob; 
    } 
    public Object get(int index) 
    { 
     if (index < 0 || index >= numElements) 
      throw new IndexOutOfBoundsException("IndexOutOfBounds"); 
     return container[index]; 
    } 
    private void resize() 
    { 
     Object[] newContainer = new Object[currSize*2]; 
     System.out.println("resize: "+ currSize); 
     for (int i=0; i < currSize; i++) 
      newContainer[i] = container[i]; 

     container = newContainer; 
     currSize *= 2; 
    } 

    public static void main(String[] args) 
    { 
     LinkedList<Marriage> myCont2 = new LinkedList<Marriage>(); 


     myCont2.add(new Marriage("Gowen", "Geter")); 
     myCont2.add(new Marriage("Holland", "Tunnell")); 
     myCont2.add(new Marriage("Daffee", "Ducmann")); 
     myCont2.add(new Marriage("Hay", "Saylors")); 
     myCont2.add(new Marriage("Rump", "Orefice")); 
     myCont2.add(new Marriage("Rump", "Hammer")); 
     myCont2.add(new Marriage("True", "Belew")); 
     myCont2.add(new Marriage("Hunting", "Hoar")); 
     myCont2.add(new Marriage("Busch", "Hacker")); 
     myCont2.add(new Marriage("Long", "Wiwi")); 
     myCont2.add(new Marriage("Fedder", "Oats")); 
     myCont2.add(new Marriage("Eggen", "Stake")); 
     myCont2.add(new Marriage("de Armendi", "Back")); 
     myCont2.add(new Marriage("Olah", "Sailer")); 
     myCont2.add(new Marriage("Burns", "Toole")); 
     myCont2.add(new Marriage("Gowen", "Geter")); 

     myCont2.add(new Marriage("Mann", "Boobs")); 
     myCont2.add(new Marriage("Cox", "Champ")); 
     myCont2.add(new Marriage("Roller", "Moore")); 
     myCont2.add(new Marriage("Achen", "Ball")); 
     myCont2.add(new Marriage("Schauer", "Bush")); 
     myCont2.add(new Marriage("Looney", "Ward")); 
     myCont2.add(new Marriage("Poore", "Sapp")); 
     myCont2.add(new Marriage("Neisser", "Ho")); 
     myCont2.add(new Marriage("Best", "Lay")); 
     myCont2.add(new Marriage("Hardy", "Harr")); 
     myCont2.add(new Marriage("Crapp", "Beer")); 

     myCont2.add(new Marriage("Traylor", "Hooker")); 
     myCont2.add(new Marriage("Wang", "Holder")); 
     myCont2.add(new Marriage("To", "Mann")); 
     myCont2.add(new Marriage("Louse", "Donge")); 
     myCont2.add(new Marriage("Fondel", "Longe")); 

     Iterator<Marriage> iter2 = myCont2.iterator(); 
     while(iter2.hasNext()) 
     { 
      System.out.println(iter2.next()); 
     } 

    } 


} 

但是,當這種打印我得到的參考ID和不在名單。有任何想法嗎?

 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

回答

3

Marriage需要正確覆蓋toString方法。

東西如下:

class Marriage { 
    String person1; 
    String person2; 

    Marriage(String p1, String p2) { 
     person1 = p1; 
     person2 = p2; 
    } 

    @Override 
    public String toString() { 
     return person1 + " " + person2; 
    } 
} 
+1

很酷的工作。謝謝! – user973858

+0

@ user973858:Np。 YW。 –

4

這是因爲的System.out.println(對象OBJ)使用的toString()的對象的方法來表示,作爲一個字符串。所以你需要做的是覆蓋Marriage類的默認toString方法,並用你自己的實現來實現它。

事情是這樣的:

class Marriage 
{ 
    public String toString() { 
     return person1 + "<->" + person2; 
    } 
} 
2

覆蓋的MarriagetoString()方法,包括要顯示的字段。目前它使用默認的toString()實現,它以十六進制的形式返回類名+其哈希碼。

您有多種選擇:IDE生成,使用番石榴的MoreObjects.toStringHelper(..),commons-lang ToStringBuilder(),或者直接手動編寫。

儘管只用於調試。

Guava docs

+0

你爲什麼說「只用於調試」? – user949300

+1

,因爲您不應該依靠'toString()'來定義表示對象的協定。 – Bozho

1

要打印的結婚對象,而不是它的內容。

考慮更改

Iterator<Marriage> iter2 = myCont2.iterator(); 
while(iter2.hasNext()) 
{ 
    System.out.println(iter2.next()); 
} 

Iterator<Marriage> iter2 = myCont2.iterator(); 
while(iter2.hasNext()) 
{ 
    iter2.next().Print(); 
} 

,然後添加打印方法婚姻:

public void Print() 
{ 
    System.out.println(person1 + " is married to " + person2 + "\n"); 
} 

,或者如果你想直接調用的println結婚對象,你上可以重寫toString方法。