0

所以我想知道是否可以在 Java 中追溯一个数组列表?意思是,如果我有类似的东西:

Section aSection = new Section();
aMainSection.get(0).aChildSection.add(aSection);

其中'aMainSection'和'aChildSection'都是类型部分的数组列表,我可以从'aChildSection'追溯以获取存储在其父部分'aMainSection'中的值吗?或者我是否必须在部分类中创建某种方法来执行此操作?感谢您提供的任何帮助。

4

2 回答 2

3

您无法从对象本身找到对对象的所有引用。您可以改为在每个孩子中记录一个“父”对象。

除了公开原始集合之外,您还可以为它们提供自定义类。

class ChildSection {
    private final Section parent;
    private final List<Section> sections = new ArraysList<Section>();

    public void add(Section section) {
        sections.add(section);
        section.setParent(parent);
    }
}
于 2011-07-08T15:11:13.007 回答
1

不,您不能使用 ArrayLists 向上跟踪,您必须在每个 Section 对象中添加父引用。

于 2011-07-08T15:10:48.770 回答