如何创建一个不指另一个的Arraylist?

dan*_*ibg 1 java clone reference arraylist copy-constructor

我知道当你创建一个ArrayList并且在声明它时你将它引用到另一个ArrayList它只引用另一个,所以对第二个引起的更改改变了第一个.但是面对这个问题我很困惑.

ArrayList <Productos> d3 = abd.dadesProductos();
ArrayList <Productos> dades2 = new ArrayList <Productos>();
System.out.println("before clear() + size= "+d3.get(i).configurables.size());//43
dades2.add(d3.get(i));
dades2.get(dades2.size()-1).configurables.clear();
System.out.println("after clear() + size= "+d3.get(i).configurables.size());//0
Run Code Online (Sandbox Code Playgroud)

问题是,在从添加到dades2的最后一项中清除参数配置(另一个arraylist)后,它也会从d3清除它,我不希望这种情况发生...为什么如果我创建一个ArrayList就会发生这种情况如 new ArrayList <Productos>();

我会提供任何帮助.

一些额外的信息.

我试图像这样创建一个新的构造函数:

Productos(Productos p)
    {
        this(p.entity_id, p.model, p.sku, p.name, p.weight, p.visibility, p.material, p.attribute_set_name, p.image, p.category_ids, p.category_ids2, p.color, p.color2, p.color3, p.cpsp_enable, p.created_at, p.description, p.colorswatch_disabled_category, p.colorswatch_disabled_product, p.msrp_display_actual_price_type, p.options_container, p.fcpm_enable, p.is_recurring, p.image_label, p.manage_stock, p.manufacturer, p.max_sale_qty, p.meta_description, p.meta_keyword, p.meta_title, p.category_positions, p.price, p.type_id, p.fcpm_second_attribute, p.fcpm_template, p.fcpm_template_position, p.short_description, p.fcpm_showfdd, p.fcpm_show_grandtotal, p.fcpm_second_layout, p.fcpm_show_link, p.fcpm_checkbox, p.fcpm_show_image, p.fcpm_show_rowtotal, p.fcpm_show_stock, p.special_price, p.special_from_date, p.special_to_date, p.status, p.tax_class_id, p.tier_price, p.url_key, p.updated_at, p.url_path, p.dimensions, p.mindeco, p.talla, p.trang1, p.trang2, p.trang3, p.trang4, p.trang5, p.tprice1, p.tprice2, p.tprice3, p.tprice4, p.tprice5, p.maxCols, p.marcPrio, p.configurables, p.marcajes);
    }
Run Code Online (Sandbox Code Playgroud)

同样的想法发生了.我认为这是因为我通过引用分配了ArrayList配置.

我试图用深度克隆做一些事但没有成功.根本不懂如何使用它.

dades2.add(new Productos(d3.get(i)));
Run Code Online (Sandbox Code Playgroud)


Productos(Productos p)
    {
        this(p.entity_id, p.model, p.sku, p.name, p.weight, p.visibility, p.material, p.attribute_set_name, p.image, p.category_ids, p.category_ids2, p.color, p.color2, p.color3, p.cpsp_enable, p.created_at, p.description, p.colorswatch_disabled_category, p.colorswatch_disabled_product, p.msrp_display_actual_price_type, p.options_container, p.fcpm_enable, p.is_recurring, p.image_label, p.manage_stock, p.manufacturer, p.max_sale_qty, p.meta_description, p.meta_keyword, p.meta_title, p.category_positions, p.price, p.type_id, p.fcpm_second_attribute, p.fcpm_template, p.fcpm_template_position, p.short_description, p.fcpm_showfdd, p.fcpm_show_grandtotal, p.fcpm_second_layout, p.fcpm_show_link, p.fcpm_checkbox, p.fcpm_show_image, p.fcpm_show_rowtotal, p.fcpm_show_stock, p.special_price, p.special_from_date, p.special_to_date, p.status, p.tax_class_id, p.tier_price, p.url_key, p.updated_at, p.url_path, p.dimensions, p.mindeco, p.talla, p.trang1, p.trang2, p.trang3, p.trang4, p.trang5, p.tprice1, p.tprice2, p.tprice3, p.tprice4, p.tprice5, p.maxCols, p.marcPrio, p.marcajes);
        ArrayList <Configurable>configs=p.configurables;
        for(int i=0;i<configs.size();i++)
        {
            this.configurables.add(new Configurable(configs.get(i)));
        }
    }
Run Code Online (Sandbox Code Playgroud)


Configurable(Configurable c)
    {
        this(c.codip, c.codic, c.sku, c.color, c.color2, c.color3, c.talla, c.price, c.image, c.trang1, c.trang2, c.trang3, c.trang4, c.trang5, c.tprice1, c.tprice2, c.tprice3, c.tprice4, c.tprice5);
    }
Run Code Online (Sandbox Code Playgroud)

因为它似乎创建了一个新的构造函数Productos是不够的,我还创建了一个新的构造函数,Configurable并在Productos构造函数上逐个复制项目.它似乎现在有效.

Dav*_*ugg 5

发生这种情况的原因是您正在改变List中的对象,而不是List本身.这两个列表中都包含相同的对象,因此当您get()从一个列表中获取同一个对象时,也会包含在另一个列表中.

解决这个问题的方法是将Productos类的副本插入到新数组中,而不是已经创建的对象中.根据Productos类的复杂程度,您可以创建一个复制构造函数,或者clone()在其上实现复制它.