Java 如何阻止对一个ArrayList中的对象的修改同时修改另一个ArrayList中的同一对象

Java 如何阻止对一个ArrayList中的对象的修改同时修改另一个ArrayList中的同一对象,java,object,arraylist,pass-by-reference,add,Java,Object,Arraylist,Pass By Reference,Add,我有下面的代码,通过比较字符串从一个arraylist中找到一个对象,然后将该对象添加到另一个arraylist中,然后在第二个arraylist中修改该对象的一个参数,但是发生的情况是,该对象的参数在两个arraylist中都被修改 我是java新手,因为java通过引用传递,所以我修改了内存中的位置,而不是它的副本,但是因为我特别是通过第二个arraylist访问修改方法(getQuantity()),所以我认为它会修改该arraylist中的对象,并且仅此而已 我在这里错过了什么?我如何克

我有下面的代码,通过比较字符串从一个arraylist中找到一个对象,然后将该对象添加到另一个arraylist中,然后在第二个arraylist中修改该对象的一个参数,但是发生的情况是,该对象的参数在两个arraylist中都被修改

我是java新手,因为java通过引用传递,所以我修改了内存中的位置,而不是它的副本,但是因为我特别是通过第二个arraylist访问修改方法(getQuantity()),所以我认为它会修改该arraylist中的对象,并且仅此而已

我在这里错过了什么?我如何克服这个问题

获取篮子()。返回存储product类型对象的arraylist,ProductList是存储product类型对象的arraylist

public void addToBasket(){

             String productName = jCustomerProductSelectionBox.getSelectedItem().toString();
             Integer quantity = Integer.valueOf(jCustomerQuanityTextField.getText());

                    castedCustomer.getTheBasket().add(theProductList.find(productName));
                    castedCustomer.getTheBasket().find(productName).setQuantity(quantity);

             }
查找的位置是:

public Product find(String nameInput){

  Product selection = null;  
    for(Product product: this.Products){

        if(product.getName().equalsIgnoreCase(nameInput)){
        selection = product;
        break;
        }  
    }    
return selection;
}

对于这里的新手问题,我深表歉意,但我已经尝试了许多不同的设置,而且似乎不起作用。

下面的答案是问题的答案之一:

无论您想在哪里获得另一个对象,都可以执行简单的克隆。e、 g:

Deletable del = new Deletable();
Deletable delTemp = (Deletable ) del.clone(); // this line will return you an independent
                                 // object, the changes made to this object will
                                 // not be reflected to other object
我没有删除我的问题,因为它的目的是帮助人们解决arraylist特定的问题,可以选择任何方式将对象从数组列表中取出,然后使用上述方法进行克隆。

在其他列表中插入对象的副本,Java不是通过引用传递的,而不仅仅是引用同一个对象。Java总是按值传递。
Deletable del = new Deletable();
Deletable delTemp = (Deletable ) del.clone(); // this line will return you an independent
                                 // object, the changes made to this object will
                                 // not be reflected to other object