如何在同一参考中设置不同的值

时间:2019-09-26 18:23:56

标签: java arraylist collections

我有三个班级TestDTOTestImplTestMainTestDTO具有数量变量,设置器和获取器方法。 TestImpl具有getTestDTO()getUpdatedTestDTO()之类的方法。

我创建了一个testdto对象,并将金额值设置为05testdto已存储了testDTO ArrayList对象。在TestImpl中,我有两个userTestcurrentTest的setter和getter。我已将testDTO ArrayList对象设置为setUserTestsetCurrentTest。当我尝试从userTestcurrentTest打印金额值时,我得到了5作为输出。

我像getUserTest().get(0).setAmount("06");一样进行注入,然后在尝试打印currentTest.getAmount()时会打印06而不是05

TestDTO:

public class TestDTO{
   private String amount;
   public void setAmount(String amount) {
      this.amount = amount;
   }
   public String getAmount() {
      return amount;
   }
}

TestImpl:

public class TestImpl {
   private ArrayList<TestDTO> userTest;
   private ArrayList<TestDTO> currentTest;

   public ArrayList<TestDTO> getTestDTO(){
      ArrayList<TestDTO> testDTO = new ArrayList<TestDTO>();
      TestDTO testdto = new TestDTO();
      testdto.setAmount("05");
      testDTO.add(testdto);
      this.setUserTest(testDTO);
      this.setCurrentTest(testDTO);
      return null;
   }
   public void getUpdateTestDTO() {
      System.out.println(this.userTest.get(0).getAmount());
      System.out.println(this.currentTest.get(0).getAmount());
   }
   public ArrayList<TestDTO> getUserTest(){
      return userTest;
   }
   public void setUserTest(ArrayList<TestDTO> userTest) {
      this.userTest = userTest;
   }
   public ArrayList<TestDTO> getCurrentTest(){
      return currentTest;
   }
   public void setCurrentTest(ArrayList<TestDTO> currentTest) {
      this.currentTest = currentTest;
   }
}

TestMain:

public class TestMain {
   TestImpl testImpl = new TestImpl();

   public static void main(String args[])
   {
      TestMain testMain = new TestMain();
      testMain.testImpl.getTestDTO();
      testMain.testImpl.getUpdateTestDTO();
      testMain.testImpl.getUserTest().get(0).setAmount("06");
      testMain.testImpl.getUpdateTestDTO();
   }
}

我希望userTest.getAmount()返回06,而currentTest.getAmount()返回05

2 个答案:

答案 0 :(得分:0)

不,先生,这不可能。设置金额时,将为相同的 testDTO 设置金额,这就是为什么您始终获得相同值的原因。

要获得预期的结果,请尝试以下解决方案。

    ArrayList<TestDTO> testDTO = new ArrayList<TestDTO>();  
    TestDTO testdto = new TestDTO();
    testdto.setAmount("05");
    testDTO.add(testdto);
    this.setUserTest(testDTO);
    ****ArrayList<TestDTO> testDTO22 = new ArrayList<TestDTO>();  
    ****TestDTO testdto22 = new TestDTO();
    ****testdto22.setAmount("05");
    ****testDTO22.add(testdto);
    ****this.setCurrentTest(testDTO22);

寻找星号标记的线。

答案 1 :(得分:0)

此问题同时存在于ArrayList userTest和currentTest中。您有一个Arraylist,用于存储指向同一对象的引用。

currentTest ---> testDTOArrayList@ioaudo
userTest ---->   testDTOArrayList@ioaudo

testDTOArrayList@ioaudo --> testDTOArrayList
testDTOArrayList ---> testdto@oisudf

testdto@oisudf ---> ---------------
                    | amount = 05 |
                    ---------------

所以我们的编译器运行了一行

testImpl.getUserTest().get(0).setAmount("06");

更改也反映在currentTest中,因为它们指向同一事物。

为避免您需要有单独的数组列表,因此一个数组中的更改不会反映在另一个数组中。

// This function will copy all values from one object and create a new object
// Stored copied values in it put in array list and return that arraylist 
public ArrayList<TestDTO> deepCopy(TestDTO testdto) {
   TestDTO testdtoCopy = new TestDTO();
   testdtoCopy.setAmount(testdto.getAmount());
   ArrayList<TestDTO> testDTOCopyArrayList = new ArrayList<TestDTO>();
   testDTOCopyArrayList.add(testdtoCopy);
   return testDTOCopy;
}

您的getTestDTO将是

public ArrayList<TestDTO> getTestDTO() {
   ArrayList<TestDTO> testDTOArrayList = new ArrayList<TestDTO>();
   TestDTO testdto = new TestDTO();
   testdto.setAmount("05");
   testDTOArrayList.add(testdto);
   this.setUserTest(testDTOArrayList);
   this.setCurrentTest(deepCopy(testdto));
   return testDTOArrayList;
}

现在两个对象将是单独的

userTest  ---> testDTOArrayList@ioaudo
currentTest ---->  testDTOCopyArray@weuirwi

testDTOArrayList@ioaudo --> testDTOArrayList
testDTOCopyArray@weuirwi ---> testDTOCopyArray

testDTOArrayList --> testdto@oisudf
testdto@oisudf ---> ---------------
                    | amount = 05 |
                    ---------------

testDTOCopyArray --> testdto@5446dfg
testdto@5446dfg --->  ---------------
                      | amount = 05 |
                      ---------------

输出:

05
05
06
05