ARRAYLIST 泛型数组问题 Java
publicclassArray{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-genera...
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.println("clear ok");
}
public ArrayList<Product> getpList() {
return pList;
}
//define any methods required here
public void add(Product myproduct){
pList.add(myproduct);
}
public Iterator createIterator(){
//return null;
InventoryIterator result =new InventoryIterator();
return result;
}
public void display(){
System.out.println(pList);
}
}
public class InventoryIterator implements Iterator {
Inventory invent=new Inventory();
public void reset() {
// TODO Auto-generated method stub
invent.display();
invent.clear();
invent.display();
}
public InventoryIterator(){
//this.pList= getpList();
}
}
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return name + " @ " + price;
}
}
输出结果
[Massage Chair @ 819.95]
[] “有问题 ”
clear ok
null “有问题”
[Massage Chair @ 819.95] “有问题”
test
reset 能调用清空函数中的输出语句 但没有清空数组
调用的DISPLAY函数中的pList 输出是空值 为什么 展开
/**
* @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.println("clear ok");
}
public ArrayList<Product> getpList() {
return pList;
}
//define any methods required here
public void add(Product myproduct){
pList.add(myproduct);
}
public Iterator createIterator(){
//return null;
InventoryIterator result =new InventoryIterator();
return result;
}
public void display(){
System.out.println(pList);
}
}
public class InventoryIterator implements Iterator {
Inventory invent=new Inventory();
public void reset() {
// TODO Auto-generated method stub
invent.display();
invent.clear();
invent.display();
}
public InventoryIterator(){
//this.pList= getpList();
}
}
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return name + " @ " + price;
}
}
输出结果
[Massage Chair @ 819.95]
[] “有问题 ”
clear ok
null “有问题”
[Massage Chair @ 819.95] “有问题”
test
reset 能调用清空函数中的输出语句 但没有清空数组
调用的DISPLAY函数中的pList 输出是空值 为什么 展开
1个回答
展开全部
//因为在你的 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 createIterator();
}
interface Iterator {
public void reset();
}
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.println("clear ok");
}
public ArrayList<Product> getpList() {
return pList;
}
// define any methods required here
public void add(Product myproduct) {
pList.add(myproduct);
}
public Iterator createIterator() {
// return null;
// 这里返回持有自己引用的InventoryIterator,这样才能清空自己
InventoryIterator result = new InventoryIterator(this);
return result;
}
public void display() {
System.out.println(pList);
}
}
class InventoryIterator implements Iterator {
Inventory invent = new Inventory();
// 添加新的构造函数,将要reset的Inventory引用传递进去.
public InventoryIterator(Inventory in) {
this.invent = in;
}
public void reset() {
// TODO Auto-generated method stub
invent.display();
invent.clear();
invent.display();
}
public InventoryIterator() {
// this.pList= getpList();
}
}
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return name + " @ " + price;
}
}
/*
* 看这里的代码
* 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 createIterator();
}
interface Iterator {
public void reset();
}
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.println("clear ok");
}
public ArrayList<Product> getpList() {
return pList;
}
// define any methods required here
public void add(Product myproduct) {
pList.add(myproduct);
}
public Iterator createIterator() {
// return null;
// 这里返回持有自己引用的InventoryIterator,这样才能清空自己
InventoryIterator result = new InventoryIterator(this);
return result;
}
public void display() {
System.out.println(pList);
}
}
class InventoryIterator implements Iterator {
Inventory invent = new Inventory();
// 添加新的构造函数,将要reset的Inventory引用传递进去.
public InventoryIterator(Inventory in) {
this.invent = in;
}
public void reset() {
// TODO Auto-generated method stub
invent.display();
invent.clear();
invent.display();
}
public InventoryIterator() {
// this.pList= getpList();
}
}
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return name + " @ " + price;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询