-1

I have two ArrayLists,

ArrayList<Item> array1 = new ArrayList<>();
// and
ArrayList<Item> array2 = new ArrayList<>();

in Item class I also have default boolean check = false;

I do checking, before everything I clean array2.clear() and required one from array1 add to array2:

array2.clear();
for (Item i : array1) {
        if (.....) {
                array2.add(i);
        }
}

If in array2 boolean check is changed from false to true it is also changed in array1. How do I keep them separate so next time I add elements to array2 boolean check wouldn't copy as true?

Edd
  • 181
  • 2
  • 14
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – Yassin Hajaj Apr 06 '16 at 21:23
  • 2
    This seems likely to be a problem with `Item`, not a problem with `ArrayList`. – Louis Wasserman Apr 06 '16 at 21:25
  • You should read about the [difference between pass-by-value and pass-by-reference](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value). Java is pass-by-reference in all cases except of primitives, which are passed by value. – user3707125 Apr 06 '16 at 21:28
  • 1
    Not that if the items in the arrays are objects, and you copy their *references* from one to the other, then both arrays reference the SAME objects. So if you change an object in one, you will see it being changed when you access it through the other one too. You can only prevent that if you actually clone the objects before you put them in the other array. – Rudy Velthuis Apr 06 '16 at 21:29
  • 2
    @user3707125 Java is always *pass-by-value* – James Buck Apr 06 '16 at 21:30
  • Did you mean `clear()`? ArrayList does not have a `clean()` method. – Andreas Apr 06 '16 at 21:33
  • @Andreas Yes, I changed it. – Edd Apr 06 '16 at 21:35
  • @JamesBuck, my bad, but I can't agree that it is pass-by-value. I think it's pass-by-pointer and pass-by-value, because the fact that all `Object`s are pointers, with automatic dereferencing during any kind of access, doesn't make it pass-by-value to me. – user3707125 Apr 06 '16 at 22:23

2 Answers2

6

You are adding the same object to two ArrayList. If you want to seperate objects create a new instance of item and add it to the array2. While creating the Item you will need a constructor which take all member's values and set it to the new object. Or you can just get the Item object and set the values in constructor.

Item class:

public class Item {

    boolean check = false;    
    //Other members 

    //Copy constructor
    public Item(Item itemToCopy) {
        check = itemToCopy.check;
        // set other members
    }
}

And the main part:

array2.clear();
for (Item i : array1) {
     if (.....) {
           //Use copy constructor
           Item newItem = new Item(i)); 
           array2.add(newItem);
     }
}
Joseph K.
  • 784
  • 1
  • 9
  • 19
  • 1
    You should suggest implementing/using either a `clone()` method or a copy constructor. – Andreas Apr 06 '16 at 21:34
  • Actually I mean copy constructor by `/*Transfer other member's values*/`. I am mentioning it know, thanks for improve my answer. – Joseph K. Apr 06 '16 at 21:36
  • 2
    No, `new Item(i.check /*Transfer other member's values*/ )` is not a copy-constructor. A copy-constructor is a constructor with a single parameter, of the same type as the class itself, i.e. `new Item(i)` would be calling a copy-constructor. – Andreas Apr 06 '16 at 21:39
  • Thank you @Andreas and Joseph B. for working solution and teaching about a copy-constructor. – Edd Apr 06 '16 at 21:56
1

You are adding element of array1 to array2 - so both arrays have reference to the same Item. You have to add copy of the item to array2, not the item itself.

abrcek
  • 93
  • 5