4

I am using two java List objects that contain some data.

the first list contains all the data objects inside it and teh second one contains some of the data(not all) from the original list.

The original list itself is a static object that can be accessed .

Here is the code below that copies the whole contents of the original list into a new List and then ammends the copied list my removing certain elements.

The issue i am having is that it seems to effect and remove same elements from the Original list!

private List<Device> deviceList;
    deviceList = App.devices;

        //check which devices have not been added by checking the position data

        for (Iterator<Device> iterator = deviceList.iterator(); iterator.hasNext(); ) {
            Device device = iterator.next();
            if (device.getPosition() != Device.NO_POSITION) {
                iterator.remove();
            }
        }
Jono
  • 17,341
  • 48
  • 135
  • 217

2 Answers2

4

In this line of code:

deviceList = App.devices;

you are not copying the list, but creating another reference to it.

To make a shallow copy of the list you can use for example: ArrayList constructor which accepts Collection as a parameter and makes copy.

So it should be like that:

private List<Device> deviceList = new ArrayList(App.devices);
Jakub H
  • 2,130
  • 9
  • 16
  • how about `deviceList = App.devices.clone()`? – dsharew Dec 19 '14 at 16:25
  • 1
    @jonney Java is not pass by reference! – Paul Boddington Dec 19 '14 at 16:29
  • @pbabcdefp just clean and **it is more explicit** _I think_ – dsharew Dec 19 '14 at 16:30
  • @jonney: This is *NOT* pass by reference. It is *creation* of reference and Java does *NOT* support pass by reference. – toddlermenot Dec 19 '14 at 16:32
  • There are tons of threads here stating it's a pass my reference an don't pass by value – Jono Dec 19 '14 at 16:43
  • Please do not use `Cloneable`, it is "secretely" depracated.. more infos [here](http://stackoverflow.com/questions/26398951/why-is-cloneable-not-deprecated) and [here](http://stackoverflow.com/questions/4592478/clone-arraylist-clone-i-thought-does-a-shallow-copy) – Menelaos Kotsollaris Dec 19 '14 at 16:54
  • @jonney: I don't know where you found this "ton of threads" in SO, while you are reading them all, please read this one too: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – toddlermenot Dec 21 '14 at 04:54
2

deviceList = App.devices; does not create a new object but it just points on the App.devices object. You could use ArrayList deviceList = new ArrayList(App.devices). This one will instatiate a new object ArrayList and it will not effect your static list object.

However, keep in mind that any change on your object Device will be applied on both on your lists too, because both objects inside those two lists are pointing on the same adress. So if you want to apply individual changes to your objects inside your deviceList, you should create new Device objects as well. You might want to take a look at Deep and Shallow copy.

Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68