所以我寫的迭代器簡短的演示和鏈表:打印出一個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]
很酷的工作。謝謝! – user973858
@ user973858:Np。 YW。 –