【发布时间】:2021-03-31 11:18:05
【问题描述】:
假设我有一个名为 Product 的类,其中包含一个像这样的 counstructor
public Product(String name, double price, int barcode){
this.name = name;
this.barcode = barcode;
this.price = price;
}
然后我有另一个类 ImportData,它有一个方法可以将大量产品加载到返回 productList 的 List 中。
我现在想从不同的类访问这个列表,而不必再次将数据加载到 csv 文件中。 所以不要这样:
ImportFruitAndVegatables iFV = new ImportFruitAndVegatables();
List<Product> products = iFV.fillListWithProducts();
我想要这样的东西
mainController.products
因为我的 mainController 已经运行了该方法。像这样
List <Product> productList = importFruitAndVegatables.fillListWithProducts();
所以我的问题是:有没有什么方法你只需要运行一次这个方法就不需要再次运行了?谢谢!
【问题讨论】:
-
有些事情我不是很清楚。
mainController是什么?您能否提供更多代码以便我们获得更多上下文?通常,您可以使用延迟初始化来执行此操作:您加载一次列表并将其存储在内存中,然后对于所有后续调用,您检查您的列表是否已经加载,如果是,则返回您在内存中的列表。
标签: java list csv memory arraylist