[英]How does Java store objects in a List?
I have to work with an existing application and there is a List which stores all rendered objects in order to remove them later if the gui changes. 我必须使用现有的应用程序,并且有一个List存储所有渲染的对象,以便稍后在gui更改时删除它们。
This is the List: 这是清单:
private List<Component> registeredComponents = new ArrayList<Component>();
Now I'm wondering if Java only stores references to the objects (ZK components) or if it does store a copy of the objects. 现在我想知道Java是否只存储对象的引用(ZK组件),或者它是否存储了对象的副本。 The question is if I should remove this.
问题是我是否应该删除它。
The list will contain references only . 该列表仅包含参考 。 This holds for all types of collections, not only ArrayLists.
这适用于所有类型的集合,而不仅仅是ArrayLists。
In Java there's actually no way to "get hold of" the object itself. 在Java中,实际上没有办法 “掌握”对象本身。 When you create a new object all you get is a reference to it (and there's no way to "dereference" it by using for instance a
*
operator as in C++). 当你创建一个新对象时,你得到的只是对它的引用(并且没有办法通过使用例如C ++中的
*
运算符来“取消引用”它)。
The List stores references to the objects, not a copy. List存储对对象的引用,而不是副本。 The object instances are shared with anyone else who happens to have another reference to them.
对象实例与恰好有另一个引用它们的任何其他人共享。
As for wanting to remove this, if you have another way to remove the objects from the GUI later (maybe you can query a parent component or something), the List may be redundant. 至于想要删除它,如果你以后有另一种方法从GUI中删除对象(也许你可以查询父组件或其他东西),列表可能是多余的。 But even if it is, there is probably not much overhead here, and not having it might be complicating your program.
但即使它是,这里可能没有太大的开销,并没有它可能会使您的程序复杂化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.