当前位置: 首页 > 知识库问答 >
问题:

每个输入ArrayList的实例化对象是否对该ArrayList使用相同的引用?

袁弘方
2023-03-14

我有一个平面文件阅读器类,它从dat文件中读取,创建存储在其唯一arraylist中的person、customer和product对象,我必须将其用于getInvoice方法。当从invoice dat文件中输入新属性时,我只为从invoice dat文件中读取的产品创建一个新的产品列表。这似乎运行良好,但是每个发票对象上的一些产品属性正在被更改。

当用product arraylist作为字段实例化新的invoice对象时,这是创建对下面代码中添加的产品列表的引用,还是创建该列表的副本?如果它只是一个参考,为什么当我在每个发票对象中阅读产品列表时,它有正确数量的产品对象每张发票(而不是所有的产品)?另外,如果我在创建一个invoice对象(将product list作为字段)后清除新的product arraylist,那么所有发票中的product list都是空的。这是为什么?如果arraylist不能工作,我还能怎么做呢?谢谢,如果需要我可以添加更多的代码。

public ArrayList<Invoice> getInvoices() {
    readPersons();
    readCustomers();
    readProducts();
    Scanner sc = null;

    try {
        sc = new Scanner(new File("data/Invoices.dat"));
        sc.nextLine(); 

        while (sc.hasNextLine()) {
            ArrayList<Product> product = new ArrayList<Product>();
            String line = sc.nextLine(); 
            String data[] = line.split(";"); 
            String invoiceCode = data[0].trim();
            String customerCode = data[1].trim();
            Customer customer = null;
            for(Customer aCustomer: customerList) {
                if (customerCode.equals(aCustomer.getCustomerCode())) {
                    customer = aCustomer;
                    break;
                }
            }
            String personCode = data[2].trim();
            Person person = null;
            for(Person aPerson: personList) {
                if (personCode.equals(aPerson.getPersonCode())) {
                    person = aPerson;
                    break;
                }
            }
            String invoiceDate = data[3];
            String products[] = data[4].split(",");
                for (int i = 0; i < products.length; i++) {
                    String productData[] = products[i].split(":");
                    for(Product aProduct: productList) {
                        if (aProduct.getProductCode().equals(productData[0])) {
                            aProduct.setInvoiceDate(this.getDateTime(invoiceDate));
                            if (productData.length == 1) {
                                aProduct.setQuantity(1);
                                product.add(aProduct);
                            } else if (productData.length == 2) {
                                aProduct.setQuantity(Integer.parseInt(productData[1]));
                                product.add(aProduct);
                            } else if (productData.length == 3) {
                                aProduct.setQuantity(Integer.parseInt(productData[1]));
                                for(Product anotherProduct: product) {
                                    if (anotherProduct.getProductCode() == productData[2]) {
                                        aProduct.setParkingPassCount(anotherProduct.getQuantity());
                                        break;
                                    }
                                }
                                product.add(aProduct);
                            }
                            break;
                        }
                    }
                }

            // Creates an Invoice object
            Invoice invoice = new Invoice(invoiceCode, invoiceDate, customer, person, product);

            // Adds the Invoice object into Invoice ArrayList
            invoiceList.add(invoice);   

        }
        sc.close();
        return invoiceList;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
public DateTime getDateTime(String Date){
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
    DateTime dateTime = formatter.parseDateTime(Date);
    return dateTime;
} 

共有1个答案

熊嘉茂
2023-03-14

不太确定你在问什么,但让我们看看;我想你的观点是:

您的类中有一些字段(invoiceList);您的方法只是将新的发票对象追加到该列表中。

假设invoiceList对象未被清除,或者其他代码调用

invoiceList = new ArrayList<>()

因此,选项一-依赖于使用一个字段,例如:

class InvoiceCollector {
   private final List<Invoice> invoices = new ArrayList<>();

   public void addNewInvoices() {
     ... reads data from file and adds new objects to invoices

   public List<Invoice> getCurrentInvoices() {
     return new ArrayList<>(invoices);

(返回该列表的副本可以防止接收该列表引用的代码在外部操作该列表)

选项二:只返回最近收集的发票(并避免在该类中包含字段):

public List<Invoice> collectInvoices() {
  List<Invoice> invoices = ...
  ... all the code you have in your method
  return invoices;
}
    null
 类似资料:
  • 假设我的代码是这样的: 简而言之,使用add()将对象添加到ArrayList时: ArrayList是“对对象的引用”的列表,还是“实际对象”的列表???

  • 我正在尝试拆分ArrayList中的每个对象,因为很多行都包含逗号(“,”)。每个对象都包含一个项和值(但并非所有对象都包含值): 这是我的一段代码: “CE”是主要的ArrayList 我的一段代码只使用逗号将其拆分为同一个ArrayList,另一个问题是如何查看没有“值”的对象并将空值添加到ArrayList。 谢谢各位。

  • 问题内容: 使用示例: 假设我有一个课堂电话。我还有另一个上课电话。 类的ArrayList为。 要遍历.. 的Arraylist而不是这样做: 我们可以像这样简单地遍历ArrayList : 现在,我要迭代并打印出所有第3个对象: 现在我的问题是: 与使用常规的for循环不同,我如何使用ArrayList迭代打印出枪支对象列表? 问题答案: 您要遵循与以前相同的模式: 在这种情况下,它将是:

  • 我有档案卡。java和Util。java,我不允许修改它。出于我自己的目的,我创建了一个类Card2,它扩展了Card,并添加了一个方法和一个方法。Util类包含一个采用ArrayList的方法 我的理解是,在这种情况下,

  • 目前,我有2个对象,我想输入一个新的对象。 我正在寻找一个关于StackOverFlow的解决方案,如下所示:但是,我不明白我的错误。 如何在Java中将对象添加到ArrayList中 下面是我的方法: 我的错误是->main.java:75:错误:找不到符号player.add(name);文字 我不明白我的问题?? 下面是我的方法: 我的类 事先谢谢你的帮助。

  • 然后是班级选拔: 教材: 因此,我主要创建新用户并将其序列化: 一切都很完美,但如果我这样做: 然后我想序列化对象用户,程序崩溃了。我得到的结果是: JAVA木卫一。NotSerializableException:在java上。木卫一。ObjectOutputStream。java上的WriteObject 0(ObjectOutputStream.java:1183)。木卫一。ObjectOu