I have faced a problem using a list in java. I have used such two lists:
List<Transaction> holdTxnList = new ArrayList<Transaction>();
List<Transaction> canceledTxnList = new ArrayList<Transaction>();
Then, I have assigned two different list into these lists with assignment operator(=) like this.
holdTxnList= A list of ‘Transaction’ Type.
canceledTxnList = Another list of ‘Transaction’ Type. I have found an interesting thing that after second assignment, both list became same with the value of last list.
I have solved the problem by using AddALL insted of assignment.
I have googled with the problem and found a a solution at following link
http://www.coderanch.com/t/402520/java/java/List-Assignment-vs-List-addAll.
Example description on the post:
First version:
The list you create with “new” in the first version is simply discarded when you make the assignment. Assigning to a Java variable in Java always completely discards any existing value without affecting it in any way. In this first version, you end up with localList pointing to the exact same List that object.getAList() returned — meaning that if “object” still has a reference to that List, code in “object” could modify that list, and the changed would be visible via localList, and vice-versa, because they reference the same object.
In the second version, the elements of “object”‘s list are put into a new list. Now either object can chnage their own list independently, without affecting the other one.
List<Transaction> holdTxnList = new ArrayList<Transaction>();
List<Transaction> canceledTxnList = new ArrayList<Transaction>();
Then, I have assigned two different list into these lists with assignment operator(=) like this.
holdTxnList= A list of ‘Transaction’ Type.
canceledTxnList = Another list of ‘Transaction’ Type. I have found an interesting thing that after second assignment, both list became same with the value of last list.
I have solved the problem by using AddALL insted of assignment.
I have googled with the problem and found a a solution at following link
http://www.coderanch.com/t/402520/java/java/List-Assignment-vs-List-addAll.
Example description on the post:
First version:
- List localList = new ArrayList();
- localList = object.getAList();
- List localList = new ArrayList();
- localList.addAll(object.getAList());
The list you create with “new” in the first version is simply discarded when you make the assignment. Assigning to a Java variable in Java always completely discards any existing value without affecting it in any way. In this first version, you end up with localList pointing to the exact same List that object.getAList() returned — meaning that if “object” still has a reference to that List, code in “object” could modify that list, and the changed would be visible via localList, and vice-versa, because they reference the same object.
In the second version, the elements of “object”‘s list are put into a new list. Now either object can chnage their own list independently, without affecting the other one.
No comments:
Post a Comment