I'm having some trouble with making list of objects based on a condition on an enum
. It seems that after I have completed the list, every item in the list is equivalent to the last item.
It's the classic case of different references pointing to the same object, but I don't know how to avoid it:
I've pared things down as much as I can while maintaining readability:
public class Foo {
Digit[] array = new Digit[2];
ArrayList<Foo> foozlets;
Foo() {
array[0] = Digit.ZERO;
foozlets = new ArrayList<Foo>();
}
Foo(Foo old, Digit num) {
this.array = old.array; \\This line is a problem, what should it say?
array[1] = num;
}
public static void main(String[] args) {
Foo f = new Foo();
System.out.println("Initial Foo:");
System.out.println(f);
f.listFoozlets();
}
void listFoozlets() {
for (Digit k : Digit.values()) {
if (k == Digit.TWO || k == Digit.FIVE) {
foozlets.add(new Foo(this, k));
System.out.println("** Foozlet being added **");
Foo foo = new Foo(this, k);
System.out.println(foo);
}
}
System.out.println("** List of Foozlets **");
for (Foo foo : foozlets) {
System.out.println(foo);
}
}
public String toString() {
return array[0].toString() + " " + array[1].toString();
}
}
enum Digit { ZERO, ONE, TWO, THREE, FOUR, FIVE }
Here is the output:
Initial Foo:
ZERO NULL
** Foozlet being added **
ZERO TWO
** Foozlet being added **
ZERO FIVE
** List of Foozlets **
ZERO FIVE
ZERO FIVE
If someone can explain why the first instance of Foo
on the list changes, and how I can make a list that doesn't change, I'd be grateful.
EDIT: Ok, I see where the problem is now. In the real, much larger program, I have a much larger array, and I want to keep the old information when I create a new Foo for the list. I've changed the code to reflect that there is additional information that I want to maintain. How do I accomplish this?