在 Java 中从 Arraylist 内引用父 Arraylist
所以我想知道Java中是否可以追踪数组列表?意思是,如果我有类似的内容:
Section aSection = new Section();
aMainSection.get(0).aChildSection.add(aSection);
其中“aMainSection”和“aChildSection”都是节类型的数组列表,我可以从“aChildSection”追溯以获取存储在其父节“aMainSection”中的值吗?或者我是否必须在节类中创建某种方法来执行此操作?感谢您提供的任何帮助。
So I was wondering if it was possible in Java to trace back up an Array List? Meaning, if I have something like:
Section aSection = new Section();
aMainSection.get(0).aChildSection.add(aSection);
Where 'aMainSection' and 'aChildSection' are both Array Lists of type section, can I trace back up from 'aChildSection' to get the values stored in it's parent section 'aMainSection'? Or do I have to create some sort of method within the section class that will do this? Thanks for any help that is offered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法从对象本身找到对该对象的所有引用。您可以改为在每个子对象中记录一个“父”对象。
您可以为它们提供自定义类,而不是公开原始集合。
You cannot find all the references to an object from the object itself. You can instead record a "parent" Object in each of the children.
Instead of exposing the raw collections you may way to have custom classes for them.
不,您无法使用 ArrayList 向上跟踪,您必须在每个部分对象中添加父引用。
No, you can't track upwards with ArrayLists, you will have to add parent references in each Section object.