ARRAYLIST 泛型数组问题 Java

来源:百度知道 编辑:UC知道 时间:2024/05/16 10:04:35
public class Array {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Inventory myInventory = new Inventory();
myInventory.add(new Product("Massage Chair", 819.95));
myInventory.display();
InventoryIterator myInventoryIterator = (InventoryIterator) myInventory.createIterator();
myInventoryIterator.reset();
myInventory.display();
System.out.println("test");
}
}

public interface Aggregate {
public Iterator createIterator();
}
interface Iterator {
public void reset();
}
import java.util.ArrayList;

public class Inventory {
private ArrayList<Product> pList=new ArrayList<Product>();

public void setpList(ArrayList<Product> pList) {
this.pList = pList;
}
public void clear(){
this.pList=null;
System.out.

//因为在你的 InventoryIterator 中调用reset时的Inventory 自己新创建出来的
/*
* 看这里的代码
* class InventoryIterator implements Iterator {

Inventory invent = new Inventory();
...
你没有将要清空的Inventory的传递进去.
还有就是 Inventory中的 createIterator(),返回的也是新的 InventoryIterator,和自己无关的Iterator
所以InventoryIterator的reset并不能起到清空创建它的Inventory的作用.
不能理解的话 请看详细代码,直接保存为Array.java, 看我程序中修改的地方已经注释好了.不懂也可M我
*
*/
import java.util.ArrayList;

public class Array {

public static void main(String[] args) {
Inventory myInventory = new Inventory();
myInventory.add(new Product("Massage Chair", 819.95));
// myInventory.display();
InventoryIterator myInventoryIterator = (InventoryIterator) myInventory
.createIterator();
myInventoryIterator.reset();
myInventory.display();
System.out.println("test");
}
}

interface Aggregate {
public Iterator