I have below two situations related to the ArrayList method get, one with a custom class and one with the String class:
1. The following is an example of modifying a Custom ArrayList element:
ArrayList<MyClass> mTmpArray1 = new ArrayList<MyClass>();
MyClass myObj1 = new MyClass(10);
mTmpArray1.add(myObj1);
MyClass myObj2 = mTmpArray1.get(0);
myObj2.myInt = 20;
MyClass myObj3 = mTmpArray1.get(0);
Log.d(TAG, "Int Value:"+myObj3.myInt);
2. The following is an example of modifying a String ArrayList element:
ArrayList<String> mTmpArray2 = new ArrayList<String>();
mTmpArray2.add("Test_10");
String myStr1 = mTmpArray2.get(0);
myStr1 = "Test_20";
String myStr2 = mTmpArray2.get(0);
Log.d(TAG, "Str Value:"+myStr2);
Therefore, in the case of MyClass ArrayList, when I call getand change the value, I see that the change is reflected when I again get.
But also, when I change the String ArrayList, the changes are not reflected.
What is the difference between the method getin both scenarios?
Is it that in the case of String, the String class creates a deep copy and returns a new object, while in the case of a custom class, a shallow copy is created?
, "LinkedHashMap", "HashMap" "List"?