tags:

views:

277

answers:

4

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;
  }
+11  A: 

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
Darn, too late :P +1 that's indeed the cause.
BalusC
+1  A: 

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
A: 

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.

ckarmann
A: 

Anytime you create a public static member of a class you are essentially creating a singleton. Here, let me show you:

public class BasicSingleton {
    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;
    private BasicSingleton m_This = new BasicSingleton();

    protected BasicSingleton(){}
    public getSingleton(){ return m_This; }
}

That's your real class. You're just hiding it.

wheaties
@wheaties - This is not thread safe :)
JonH
Yup, you're right. But honestly, mutable singletons aren't thread safe regardless. This was merely an attempt to have the OP visualize what his code really meant.
wheaties