ArrayList problem while deleating
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 = TempAr开发者_开发知识库ray;
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
.
精彩评论