hi i have a problem with Arraylist objects here is my code
ArrayList<String> globlalArrayList = new ArrayList<String>(); //declared in some other class
ArrayList<String> TempArray = getsomeTempObjects()//method
globlalArrayList = TempArray;
TempArray.clear(); //Here the Problem
in above code i want to clear all TempArray obj. but it is also clearing globlalArrayList Can Any one tell me what happening and how can i achieve this problem
You should not use just reference copy. Use addAll():
globlalArrayList.addAll(TempArray);
What you do now is just referencing one object from two references. If you make manipulation one, it is all refletected on the second, as they reference to 1(one) object.
P.S. Java uses camelCase style, so please name your array as tempArray.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With