gpt4 book ai didi

java - 如何在同一个引用中设置不同的值

转载 作者:行者123 更新时间:2023-12-02 00:10:48 24 4
gpt4 key购买 nike

我有三个类 TestDTOTestImplTestMainTestDTO 有 amount 变量、setter 和 getter 方法。 TestImpl 具有 getTestDTO()getUpdatedTestDTO() 等方法。

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

我像 getUserTest().get(0).setAmount("06"); 那样注入(inject),然后当我尝试打印 currentTest.getAmount() 时,它会打印 06而不是05

测试DTO:

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

测试Impl:

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;
}
}

测试主程序:

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() 返回 06currentTest.getAmount() 返回 05

最佳答案

该问题存在于 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");

更改也反射(reflect)在 currentTest 中,因为它们指向相同的事情。

为了避免这种情况,您需要有单独的数组列表,这样一个数组中的更改就不会反射(reflect)在另一个数组中。

// 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

关于java - 如何在同一个引用中设置不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58122684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com