ArrayList add() 函数正在复制我的数据
ArrayList add() function is duplicating my data
标题可能有点混乱,所以我将通过代码演示发生了什么。我过去使用过 ArrayLists,所以我要么遗漏了一些非常明显的东西,要么我认为这与我的 Java 版本 (jdk1.7.0_75/jre1.7.0) 有关。
public void test()
{
List<Objects> horse = new ArrayList<Objects>();
Objects OBJECT = new Objects(63, 63, 63, 0, 1, 10, 0);
Objects OBJECT2 = new Objects(64, 64, 64, 0, 2, 10, 0);
Objects OBJECT3 = new Objects(77, 77, 77, 0, 1, 10, 0);
horse.add(OBJECT);
horse.add(OBJECT2);
horse.add(OBJECT3);
for (Objects o : horse)
{
System.out.println(o.objectX);
}
}
输出为:
77
77
77
Objects class:
package server.model.objects;
public class Objects {
public long delay, oDelay;
public int xp, item, owner, target, times;
public boolean bait;
public static int objectId;
public static int objectX;
public static int objectY;
public int objectHeight;
public int objectFace;
public int objectType;
public int objectTicks;
public static int getObjectId() {
return objectId;
}
public static int getObjectX() {
return objectX;
}
public static int getObjectY() {
return objectY;
}
public Objects(int id, int x, int y, int height, int face, int type, int ticks) {
Objects.objectId = id;
Objects.objectX = x;
Objects.objectY = y;
this.objectHeight = height;
this.objectFace = face;
this.objectType = type;
this.objectTicks = ticks;
}
public int getObjectHeight() {
return this.objectHeight;
}
public int getObjectFace() {
return this.objectFace;
}
public int getObjectType() {
return this.objectType;
}
}
感谢所有帮助。谢谢你的时间。
objectX
是您代码中的静态变量,这意味着无论何时将其绑定到 Objects
class 作为一个整体,而不是任何特定实例,并将其更改为一个位置到处都改变它。
让它不是静态变量应该可以解决问题。
标题可能有点混乱,所以我将通过代码演示发生了什么。我过去使用过 ArrayLists,所以我要么遗漏了一些非常明显的东西,要么我认为这与我的 Java 版本 (jdk1.7.0_75/jre1.7.0) 有关。
public void test()
{
List<Objects> horse = new ArrayList<Objects>();
Objects OBJECT = new Objects(63, 63, 63, 0, 1, 10, 0);
Objects OBJECT2 = new Objects(64, 64, 64, 0, 2, 10, 0);
Objects OBJECT3 = new Objects(77, 77, 77, 0, 1, 10, 0);
horse.add(OBJECT);
horse.add(OBJECT2);
horse.add(OBJECT3);
for (Objects o : horse)
{
System.out.println(o.objectX);
}
}
输出为:
77
77
77
Objects class:
package server.model.objects;
public class Objects {
public long delay, oDelay;
public int xp, item, owner, target, times;
public boolean bait;
public static int objectId;
public static int objectX;
public static int objectY;
public int objectHeight;
public int objectFace;
public int objectType;
public int objectTicks;
public static int getObjectId() {
return objectId;
}
public static int getObjectX() {
return objectX;
}
public static int getObjectY() {
return objectY;
}
public Objects(int id, int x, int y, int height, int face, int type, int ticks) {
Objects.objectId = id;
Objects.objectX = x;
Objects.objectY = y;
this.objectHeight = height;
this.objectFace = face;
this.objectType = type;
this.objectTicks = ticks;
}
public int getObjectHeight() {
return this.objectHeight;
}
public int getObjectFace() {
return this.objectFace;
}
public int getObjectType() {
return this.objectType;
}
}
感谢所有帮助。谢谢你的时间。
objectX
是您代码中的静态变量,这意味着无论何时将其绑定到 Objects
class 作为一个整体,而不是任何特定实例,并将其更改为一个位置到处都改变它。
让它不是静态变量应该可以解决问题。