元素添加到ArrayList和它取代Java中的所有previous元素 [英] Add elements to Arraylist and it replaces all previous elements in Java

查看:114
本文介绍了元素添加到ArrayList和它取代Java中的所有previous元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加元素到ArrayList和它正确添加第一个,但后来当我添加抹布用从最近添加的价值的其他内容的任何后续元素,并增加了一个新的元素到ArrayList

我跑了使用ArrayList和整数,甚至另一个创建的类测试,它完美地工作,但一些事情,我在这里使用自定义类引起的问题。

在code为数组列表

 公共静态无效的主要(字符串ARGS []){
   清单< BasicEvent>名单=新的ArrayList< BasicEvent>();
   list.add(新BasicEvent(基本,门,9,4444,新的Date(12,04,2010),新的时间(12,04,21),1,0.98,0));
   list.add(新BasicEvent(复合,门,125,4444,新的Date(12,04,2010),新的时间(12,04,20),1,0.98,1));
   list.add(新BasicEvent(基本,门,105,88,新的Date(12,04,2010),新的时间(12,05,23),1,0.98,0));
   list.add(新BasicEvent(基本,门,125,12,新日期(12,04,2010),新的时间(12,05,28),1,0.98,1));
   list.add(新BasicEvent(基本,门,129,25,新的Date(12,04,2010),新的时间(12,05,30),1,0.98,0));
   list.add(新BasicEvent(基本,门,125,63,新的Date(12,04,2010),新的时间(12,04,20),1,0.98,1));
   list.add(新BasicEvent(基本,检测,127,如图9所示,新的日期(12,04,2010),新的时间(12,05,29),1,0.98,-1));   的for(int i = 0; I<则为list.size();我++){的System.out.println(列出poition+ I +就是+ BasicEvent.basicToString(list.get(I)));}

而code为自定义类basicEvent是

 公共类BasicEvent {
  公共静态字符串水平;
  公共静态字符串ETYPE;
  公共静态双XPOS;
  公共静态双yPos;
  公共静态日期日期;
  公共静态时的时间;
  公共静态双RLB;
  公共静态双西格;
  公共静态int保留;  公共BasicEvent(字符串L,串E,双X,双Y,日期D,时间T,双R,双S,诠释RES){
   等级= L;
   ETYPE = E;
   XPOS = X;
   yPos = Y;
   日期= D;
   时间= T;
   RLB = R;
   SIG = S;
   预留= RES;
  }; 公共静态字符串basicToString(BasicEvent BSE){
   串出= bse.getLevel()+; + bse.getEtype()+; + bse.getxPos()+; + bse.getyPos()+; + bse.getDate()dateAsString()+; + bse.getTime()timeAsString()+; + bse.getRlb()+; + bse.getSig()+; + bse.getReserved();
   返回的;
  }


解决方案

类的所有成员 BasicEvent 是静态的,即它们是在类的所有实例之间共享。因此,当你创建一个新的实例,旧实例的属性被覆盖的新值。

您应该类定义更改为

 公共类BasicEvent {
  公共字符串等级;
  公共字符串协议类型;
  公共双XPOS;
  公共双yPos;
  公开日期日期;
  公共时间的时间;
  公共双RLB;
  公共双西格;
  公众诠释保留;
  ...
}

作为一个方面说明,一般是不使用公共领域的良好实践 - 更好地使他们的私人和提供仅作为所需的公共访问/ setter方法​​。当然,在实验code它没有多大关系,但在产品质量code它。

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

解决方案

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.

这篇关于元素添加到ArrayList和它取代Java中的所有previous元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆