gpt4 book ai didi

java - 该对象内的对象的数组列表

转载 作者:行者123 更新时间:2023-11-29 07:45:47 25 4
gpt4 key购买 nike

假设我使用如下代码:

public class MyObject {
private int id;
private ArrayList<MyObject> otherObjects;

public MyObject(int id) {
this.id = id;
ArrayList<MyObject> otherObjects = new ArrayList<MyObject>();
otherObjects.add(new MyObject(id + 1));
this.otherObjects = otherObjects;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public ArrayList<MyObject> getOtherObjects() {
return otherObjects;
}

public void setOtherObjects(ArrayList<MyObject> otherObjects) {
this.otherObjects = otherObjects;
}
}

我应该注意哪些陷阱?我的另一个担心是,如果我可以让这个类 Parcellable?

最佳答案

如果你用不同的线程运行它,你就会有数据竞争,因为你没有锁定你的全局变量。

关于java - 该对象内的对象的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25961909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com