我在使用Java时遇到了麻烦。
我有两个班级, suprad 和 coupling 。
耦合由 suprad's 的ArrayList(列表称为 supradscoupled )和两个值(整数 num 和double q )
每个实例化的耦合都写入一个名为 Createdcouplings 的ArrayList中。
在主程序中,我必须声明两个 suprad's 之间的两个 couplings (总共4个 suprad )。当我声明第一个耦合(由两个首个 suprad 组成)时,没有问题。
但是,当我声明第二个槽在ToBeCoupledSupRads ArrayList上添加其他两个suprad时,旧的耦合将被清除,然后由新的耦合覆盖。
最后,我得到了两个仅由新的耦合形成的重复耦合。
MainWindow类:
public class MainWindow {
(...)
public static List<suprad> ToBeCoupledSupRads = new ArrayList<suprad>();
public static void main(){
(...)
int i = ToBeCoupledComboBox.getSelectedIndex();
suprad stemp = suprad.Createdsuprads.get(i);
(...)
// adding new suprad to this ArrayList makes the ArrayList Createdcouplings have its registered data overwritten!
ToBeCoupledSupRads.add(stemp);
(...)
}
}
耦合类:
public class coupling {
public int num;
public static int total = 0;
public double q;
public static List<coupling> Createdcouplings = new ArrayList<coupling>();
public List<suprad> supradscoupled = new ArrayList<suprad>();
public coupling(List<suprad> nums,double q)
{
(...)
this.supradscoupled = aux;
total++;
this.num = total;
this.q=q;
// this is the ArrayList that is having its older data cleared and overwritten unwantedly
Createdcouplings.add(this);
}
}
我希望获得一些帮助,该Java项目正在使用带有IntelliJ IDEA社区的JavaSDK 1.8.0_221进行编译。