The reason is that when passing an ArrayList as an argument, the called method can modify the contents of the array. ArrayList contains references to objects. If you want to avoid some class changing the contents of your ArrayList, you have to return a copy of your ArrayList, where all the objects inside are clones of the () s objects in your list.
Use object.clone ()
ArrayList clonedCopy = new ArrayList(list1.size()); for (Object obj : list1) { clonedCopy.add(obj.clone()); }
Now return this cloned Mine. But make sure obj is cloned!
source share