简体   繁体   English

Java如何在List中存储对象?

[英]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.

相关问题 如何在java中存储和读取对象的数组列表? - How to store and read an array list of objects in java? 如何在Java中存储worker对象列表? - How to store list of workers objects in java? 存储常量(Java 对象)是否需要堆? 多少钱? - Does it take heap to store constants (Java objects)? And how much? 如何在Solr上存储java对象 - How to store java objects on Solr 在Redis中存储Java对象列表的最佳方法 - Best way to store a list of java objects in Redis Java:使用反射还是将实例对象存储在列表中? - Java: Use reflection or store instance objects in list? Java:存储和访问对象列表的最佳方式 - Java: Best way to store and access a list of objects ORMLite不知道如何存储接口java.util.List - ORMLite does not know how to store interface java.util.List 如何在 java class 中最好地存储来自 CSV 的数据? 一个 Row 对象列表,还是一个带有嵌套对象的 object? - How to best store data from CSV in java class? A single list of Row objects, or a single object with nested objects? Spark Task Executors工作时如何在Java并发Java List中存储多个json对象 - How to store multiple json objects in java Concurrent Java List while Spark Task Executors do work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM