gpt4 book ai didi

java - 打印出一个链表

转载 作者:行者123 更新时间:2023-12-02 20:35:09 26 4
gpt4 key购买 nike

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

    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@6bbc4459Marriage@152b6651Marriage@544a5ab2Marriage@5d888759Marriage@2e6e1408Marriage@3ce53108Marriage@6af62373Marriage@459189e1Marriage@55f33675Marriage@527c6768Marriage@65690726Marriage@525483cdMarriage@2a9931f5Marriage@2f9ee1acMarriage@67f1fba0Marriage@3fbefab0Marriage@133c5982Marriage@5f186fabMarriage@3d4b7453Marriage@24c21495Marriage@41d5550dMarriage@1cc2ea3fMarriage@40a0dcd9Marriage@1034bb5Marriage@7f5f5897Marriage@4cb162d5Marriage@11cfb549Marriage@5b86d4c1Marriage@70f9f9d8Marriage@2b820ddaMarriage@675b7986Marriage@2687816d

最佳答案

这是因为 System.out.println(Object obj) 使用对象的 toString() 方法将其表示为 String。因此,您需要做的是重写 Marriage 类的默认 toString 方法,并使用您自己的实现来实现它。

类似这样的事情:

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

关于java - 打印出一个链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8250348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com