将元素添加到 Arraylist 并替换 Java 中所有以前的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2447942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add elements to Arraylist and it replaces all previous elements in Java
提问by pie154
I am adding elements to a ArrayList and it adds the first one correctly but then when I add any subsequent elements it wipes replaces the other elements with the value from the most recently added and adds a new element to the ArrayList.
我正在向 ArrayList 添加元素,它正确添加了第一个元素,但是当我添加任何后续元素时,它会用最近添加的值替换其他元素,并向 ArrayList 添加一个新元素。
I ran test using arraylist and ints and even another created class and it worked perfectly but something about the custom class I am using here causes problems.
我使用 arraylist 和 ints 甚至另一个创建的类运行了测试,它运行良好,但我在这里使用的自定义类的某些方面会导致问题。
The code for the array list is
数组列表的代码是
public static void main(String args[]){
List<BasicEvent> list = new ArrayList<BasicEvent>();
list.add(new BasicEvent("Basic", "Door", 9, 4444, new Date(12,04,2010), new Time(12,04,21), 1, 0.98, 0));
list.add(new BasicEvent("Composite", "Door", 125, 4444, new Date(12,04,2010), new Time(12,04,20), 1, 0.98, 1));
list.add(new BasicEvent("Basic", "Door", 105, 88, new Date(12,04,2010), new Time(12,05,23), 1, 0.98, 0));
list.add(new BasicEvent("Basic", "Door", 125, 12, new Date(12,04,2010), new Time(12,05,28), 1, 0.98, 1));
list.add(new BasicEvent("Basic", "Door", 129, 25, new Date(12,04,2010), new Time(12,05,30), 1, 0.98, 0));
list.add(new BasicEvent("Basic", "Door", 125, 63, new Date(12,04,2010), new Time(12,04,20), 1, 0.98, 1));
list.add(new BasicEvent("Basic", "Detect", 127, 9, new Date(12,04,2010), new Time(12,05,29), 1, 0.98, -1));
for(int i=0;i<list.size();i++) {System.out.println("list a poition " + i + " is " + BasicEvent.basicToString(list.get(i)));}
And the code for the custom class basicEvent is
自定义类 basicEvent 的代码是
public class BasicEvent {
public static String Level;
public static String EType;
public static double xPos;
public static double yPos;
public static Date date;
public static Time time;
public static double Rlb;
public static double Sig;
public static int Reserved;
public BasicEvent(String L, String E, double X, double Y, Date D, Time T, double R, double S, int Res){
Level = L;
EType = E;
xPos = X;
yPos = Y;
date = D;
time = T;
Rlb = R;
Sig = S;
Reserved = Res;
};
public static String basicToString(BasicEvent bse){
String out = bse.getLevel() + ";" + bse.getEtype() + ";" + bse.getxPos() + ";" + bse.getyPos() + ";" + bse.getDate().dateAsString() + ";" + bse.getTime().timeAsString() + ";" + bse.getRlb() + ";" + bse.getSig() + ";" + bse.getReserved();
return out;
}
回答by Péter T?r?k
All the members of your class BasicEventare static, i.e. they are shared between all instances of the class. Thus when you create a new instance, the properties of the old instance are overridden with the new values.
类的所有成员BasicEvent都是静态的,即它们在类的所有实例之间共享。因此,当您创建新实例时,旧实例的属性将被新值覆盖。
You should change your class definition to
您应该将类定义更改为
public class BasicEvent {
public String Level;
public String EType;
public double xPos;
public double yPos;
public Date date;
public Time time;
public double Rlb;
public double Sig;
public int Reserved;
...
}
As a side note, in general it is not good practice to use public fields - better make them private and provide public accessors / setters only as needed. Of course, in experimental code it does not matter much, but in production quality code it does.
附带说明一下,通常使用公共字段不是一个好习惯——最好将它们设为私有并仅在需要时提供公共访问器/设置器。当然,在实验代码中它并不重要,但在生产质量代码中它确实如此。
回答by Alexander Malfait
Why are all your class members static?
为什么你所有的班级成员都是静态的?
Static means there will be only one value in the whole VM, so it's logical the values are being overwritten on each instance creation (this is not a problem with ArrayList).
静态意味着整个 VM 中只有一个值,因此在每个实例创建时这些值都被覆盖是合乎逻辑的(这不是 ArrayList 的问题)。
Make your member variables non-static, and consider making them private and exposing them with getters.
使您的成员变量非静态,并考虑将它们设为私有并使用 getter 公开它们。
回答by Cyrille Ka
Well, all your fields in BasicEvent are static, so they belong to the class, not to objects. That means they are the same for all objects. Every time you create an object, you write on these fields.
好吧,BasicEvent 中的所有字段都是静态的,因此它们属于类,而不属于对象。这意味着它们对于所有对象都是相同的。每次创建对象时,都会在这些字段上进行写入。
Please look at your Java documention on the signification of static fields and how to use them.
请查看您的 Java 文档,了解静态字段的含义以及如何使用它们。

