Add elements to Arraylist and it replaces all previous elements in Java
Posted
by pie154
on Stack Overflow
See other posts from Stack Overflow
or by pie154
Published on 2010-03-15T14:48:13Z
Indexed on
2010/03/15
14:49 UTC
Read the original article
Hit count: 328
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 form 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 soemthing about the custon 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; }
© Stack Overflow or respective owner