开发者

Add elements to Arraylist and it replaces all previous elements in Java [duplicate]

开发者 https://www.devze.com 2022-12-23 13:05 出处:网络
This question already has answers here: Why does my ArrayList contain N copies of the last item added to the list?
This question already has answers here: Why does my ArrayList contain N copies of the last item added to the list? (5 answers) Closed 9 years ago.

I am adding elements to a ArrayList and it adds the 开发者_Go百科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.

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.

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

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;
  }


All the members of your class BasicEvent are 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.

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.


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).

Make your member variables non-static, and consider making them private and exposing them with getters.


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.

Please look at your Java documention on the signification of static fields and how to use them.

0

精彩评论

暂无评论...
验证码 换一张
取 消