【问题标题】:Lookup item in an ArrayList and output在 ArrayList 中查找项目并输出
【发布时间】:2016-04-28 04:08:42
【问题描述】:

您好,如果用户输入与 ArrayList 中的 itemNumber 匹配,我正在尝试从 arrayList 输出项目。我正在使用 an 并且这些是覆盖方法。

实现的接口方法:

package Purchase;

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

String description;


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

【问题讨论】:

  • 你能澄清你的问题吗?你想替换返回“不存在”;然后将不存在的对象添加到列表中?
  • @Kiresays 不,我的问题是,当我希望它从 ArrayList 返回项目描述时,它正在返回“不存在”。

标签: java loops for-loop arraylist interface


【解决方案1】:

你的问题在这里:

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();

在您的实体类中。

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多