Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在另一个类的参数内将勘误表设置为空的最佳方法_Java_Arrays_List_Class - Fatal编程技术网

Java 在另一个类的参数内将勘误表设置为空的最佳方法

Java 在另一个类的参数内将勘误表设置为空的最佳方法,java,arrays,list,class,Java,Arrays,List,Class,在这个类中,我要调用数组,并在参数中将数组设置为空 public class ElectronicsEquipmentSupplier { private int currentMonth; private int currentYear; private String rangeOfProducts; private CustomerDetailsList details; //Contains the customer deta

在这个类中,我要调用数组,并在参数中将数组设置为空

public class ElectronicsEquipmentSupplier {
        private int currentMonth;
        private int currentYear;
        private String rangeOfProducts;
        private CustomerDetailsList details; //Contains the customer details array
        private PurchaseOrderList pal; //Contains the purchase array

        public ElectronicsEquipmentSupplier(int currentMonth, int currentYear,
                String rangeOfProducts ) {
            this.currentMonth = currentMonth;
            this.currentYear = currentYear;
            this.rangeOfProducts = rangeOfProducts;  
        }
    }
这是创建数组的类。它从一个名为PurchaseOrder的单独类中提取信息,然后设置列表

public class PurchaseOrderList {

    private ArrayList<PurchaseOrder> purchaseCollection;

    public PurchaseOrderList() {
        purchaseCollection = new ArrayList<PurchaseOrder>();
    }
公共类PurchaseOrderList{
私人ArrayList purchaseCollection;
公共采购订单列表(){
purchaseCollection=new ArrayList();
}

CustomerDetailsList类本质上是相同的。只是不确定在ElectronicsEquipmentSupplier中调用时将数组设置为空的最佳方法。

只需在PurchaseOrderClass中使用一个可公开访问的方法包装集合自己的
clear()
方法:

public class PurchaseOrderList {

    private ArrayList<PurchaseOrder> purchaseCollection;

    public PurchaseOrderList() {
        purchaseCollection = new ArrayList<PurchaseOrder>();
    }

    //THIS IS THE IMPORTANT PART
    public void clearPurchaseCollection() {
        purchaseCollection.clear();
        //You could also accomplish the same thing by reinitializing the list:
        //purchaseCollection = new ArrayList<PurchaseOrder>();
    }

}
公共类PurchaseOrderList{
私人ArrayList purchaseCollection;
公共采购订单列表(){
purchaseCollection=new ArrayList();
}
//这是重要的部分
公开作废clearPurchaseCollection(){
purchaseCollection.clear();
//您还可以通过重新初始化列表来完成相同的任务:
//purchaseCollection=new ArrayList();
}
}
但是请注意,调用
new PurchaseOrderList()
已经保证了一个空的
purchaseCollection
列表,因为您用这种方式在构造函数中初始化它


因此,您需要调用
clearPurchaseCollection()
的唯一时间是,如果您正在重用此对象,并希望首先清除它。根据应用程序的其他部分,这可能是必要的,但丢弃该实例并创建
新的PurchaseOrderList()可能更简单
。完全取决于情况。

我不确定调用时的
参数中的
是什么意思,因此我想在参数中将数组的实例引入ElectronicsEquipmentSupplier,并将其设置为empty可能我只是说错了。在另一个类别中清空数组的最佳方法是什么没有调用空数组方法的ss?为什么不调用clear()呢方法?分配一个新的ArrayList始终是一个选项。另外,如果PurchaseOrderList只包含一个PurchaseOrderList,为什么不在ElectronicsEquipmentSupplier上添加它呢?回答不错。可能值得一提的是,它隐藏了PurchaseOrderList中集合的内部。如果集合更改为集合,例如,客户端代码保持不变,只调用clearPurchaseCollection()。也许还值得一提的是,PurchaseOrderList决不能泄露对内部集合的引用。哦,太棒了。那么我该如何使数组用于此类中呢?因为我已经创建了包含它们的类的对象,例如私有CustomerDetails;@SmallLegend:我不确定我是否完全理解您的问题,但我想您是在问如何初始化CustomerDetailsList和PurchaseOrderList对象中的内部集合。对吗?