3

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.

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;
  }
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
pie154
  • 603
  • 3
  • 10
  • 28
  • Try to reformat the code of BasicEvent so that it looks as actual code in the question, thanks. – Cyrille Ka Mar 15 '10 at 14:50
  • What do you get as output, and what do you expect ? – chburd Mar 15 '10 at 14:51
  • 1
    Why are your class members static? – JonH Mar 15 '10 at 14:51
  • 1
    `new Date(12,04,2010)` has at least 2 errors in it, probably 3. `2010` does not indicate the year you think it does in that case, `04` is a octal binary, as soon as you reach `08` you'll notice the problem, `04` (as well as `4`) probably doesn't indicate the month you think it does, check the Javadoc of that constructor. – Joachim Sauer Mar 15 '10 at 14:53

3 Answers3

14

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.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
1

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.

Alexander Malfait
  • 2,691
  • 1
  • 23
  • 23
0

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.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58