Eri*_*son 2 java enums reference
我在根据条件制作对象列表时遇到了一些麻烦enum
.似乎在我完成列表后,列表中的每个项目都等同于最后一项.
这是指向同一个对象的不同引用的经典案例,但我不知道如何避免它:
在保持可读性的同时,我尽可能地减少了事情:
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 }
Run Code Online (Sandbox Code Playgroud)
这是输出:
Initial Foo:
ZERO NULL
** Foozlet being added **
ZERO TWO
** Foozlet being added **
ZERO FIVE
** List of Foozlets **
ZERO FIVE
ZERO FIVE
Run Code Online (Sandbox Code Playgroud)
如果有人可以解释为什么Foo
列表中的第一个实例发生了变化,以及我如何制作一个不会改变的列表,我将不胜感激.
编辑: 好的,我现在看到问题所在.在真正的,更大的程序中,我有一个更大的数组,并且我想在为列表创建新的Foo时保留旧信息.我已经更改了代码以反映我想要维护的其他信息.我该如何做到这一点?
这个位是罪魁祸首:
Foo(Foo old, Digit num) {
this.array = old.array;
array[0] = num;
}
Run Code Online (Sandbox Code Playgroud)
您正在复制对旧Foo数组的引用,然后更改该数组中的值.
为什么你甚至有一个大小为1的数组而不是一个Digit
?如果你真的想要这个数组,你可能想要克隆它而不是仅仅复制引用,但是我们无法确定它的用意是什么.
这是一个较短的例子:
enum Digit { ZERO, ONE, TWO, THREE, FOUR, FIVE }
public class Foo {
Digit[] array = new Digit[1];
Foo() {
array[0] = Digit.ZERO;
}
Foo(Foo old, Digit num) {
this.array = old.array;
array[0] = num;
}
public String toString() {
return array[0].toString();
}
public static void main(String[] args) {
Foo f = new Foo();
System.out.println(f);
Foo other = new Foo(f, Digit.ONE);
System.out.println(f);
}
}
Run Code Online (Sandbox Code Playgroud)