嗨,我正在尝试从arrayList输出项目,如果用户输入与ArrayList中的itemNumber相匹配。我正在使用和这些是重写方法。

实现的接口方法:

package Purchase;

import java.util.*;
public class Items implements recordItem {

String description;


ArrayList<Items> itemList = new ArrayList<Items>();
public static Items newItems = new Items();

最佳答案

您的问题在这里:

this.itemNumber = itemNumber;
this.description = description;
this.unitPrice = unitPrice;
this.sort = sort;

Items theItems = new Items();
itemList.add(theItems);


您正在向列表添加一个空的“项目”。

这将起作用:

Items theItems = new Items();

theItems.itemNumber = itemNumber;
theItems.description = description;
theItems.unitPrice = unitPrice;
theItems.sort = sort;

itemList.add(theItems);


并且请不要使用静态实例,例如

public static Items newItems = new Items();


在您的实体类中。

07-24 09:27