打印出链接列表

时间:2011-11-23 23:12:35

标签: java

所以我正在编写迭代器和链表的简短演示:

    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,而不是列表。有什么想法吗?

Marriage@6bbc4459
Marriage@152b6651
Marriage@544a5ab2
Marriage@5d888759
Marriage@2e6e1408
Marriage@3ce53108
Marriage@6af62373
Marriage@459189e1
Marriage@55f33675
Marriage@527c6768
Marriage@65690726
Marriage@525483cd
Marriage@2a9931f5
Marriage@2f9ee1ac
Marriage@67f1fba0
Marriage@3fbefab0
Marriage@133c5982
Marriage@5f186fab
Marriage@3d4b7453
Marriage@24c21495
Marriage@41d5550d
Marriage@1cc2ea3f
Marriage@40a0dcd9
Marriage@1034bb5
Marriage@7f5f5897
Marriage@4cb162d5
Marriage@11cfb549
Marriage@5b86d4c1
Marriage@70f9f9d8
Marriage@2b820dda
Marriage@675b7986
Marriage@2687816d

4 个答案:

答案 0 :(得分:4)

那是因为 System.out.println(Object obj)使用对象的 toString()方法将其表示为 String < / strong>即可。所以你需要做的是覆盖Marriage类的默认toString方法,并用你自己的实现来实现它。

这样的事情:

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

答案 1 :(得分: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;
    }
}

答案 2 :(得分:2)

覆盖toString()的{​​{1}}方法,并包含您要显示的字段。目前,它使用默认的Marriage实现,它以十六进制形式返回类名+其哈希码。

你有多种选择:IDE生成,使用guava的toString(),commons-lang MoreObjects.toStringHelper(..),或者只是手动编写。

但仅用于调试。

Guava docs

答案 3 :(得分:1)

您正在打印Marriage对象而不是其内容。

考虑改变

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方法。