Java中删除对象

发布于 2024-10-03 12:28:20 字数 249 浏览 4 评论 0原文

所以在我的程序中,我将几个不同的对象存储到一个数组中。我随机生成一个数字,作为数组的索引,并克隆该对象(九次)以在屏幕上创建 3x3 的对象网格。

现在我的问题是在我拥有 3x3 网格后,我希望能够擦除它并生成数组中的新对象网格。问题是我想不出如何摆脱所有这些克隆,除了将它们移出显示器之外,这似乎浪费内存。我想做 400 次试验,这样到最后就会有很多克隆对象。

有没有办法删除这些克隆对象?我必须创建新对象,因为数组中的对象之一可能在网格中使用两次。

So in my program I have several different objects stored into an array. I randomly generate a number that is an index into the array and clone that object (nine times) to make a 3x3 grid of objects on my screen.

Now my problem is after I have my 3x3 grid I want to be able to erase that and generate a new grid of objects in that are in the array. The problem is I can't think of how to get rid of all these clones except for just moving them out of the display which seems like a waste of memory. I want to do like 400 trials, so that would be a lot of cloned objects by the end.

Is there a way I can delete these cloned objects? I have to create new objects, because it is possible that one of the objects in my array gets used twice in the grid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦忆晨望 2024-10-10 12:28:20

我认为java垃圾收集器会为你做这件事。
当一个对象没有被任何人引用时,当系统识别到这一点时,它将被删除;但是当你说“将它们移出显示器”时是什么意思?如果它们是“图形对象”,它们应该位于其他对象(如 JFrame)内,因此您必须使用一种方法来告诉 JFrame 对象(或其他对象)丢弃引用或用另一个对象覆盖引用。

I think java garbage collector will do it for you.
When an object isn't referenced from anyone it will be deleted when the system recognize this; but what do you mean when you say "move them out of the display"? If they are "graphic object" they should be inside other object (like JFrame) so you have to use a method to tell the JFrame object (or other) to throw the reference away or overwrite the reference with another one.

情域 2024-10-10 12:28:20

因为引用仍然保留在容器内。我认为你已经做了类似的事情: myJFrameObject.getContentPane.add(object_to_display);因此引用位于 myJFrameObject 内部,您可以使用 myJFrameObject.getContentPane.remove 例如。如果需要,可以使用 setVisible(false) 隐藏框架。这取决于具体情况。

Because the reference still remains inside the container. I think you have done anything like: myJFrameObject.getContentPane.add(object_to_display); so the reference is inside myJFrameObject, and you can use myJFrameObject.getContentPane.remove for example. If you want you can use setVisible(false) to hide the frame. It depends on the situation.

七七 2024-10-10 12:28:20

它有助于认识到您处理的不是实际的对象,而是内存中对象的指针或引用。所以:

Object obj = new Object();

不是一个对象,而是指向内存中对象的链接。通过将链接设置为 null 或将其替换为另一个对象的链接来删除链接,旧对象将被删除......ish。当垃圾收集运行时,它将被删除。所以:

obj = null;

并且

obj = new Object();

本质上会删除旧对象。这同样适用于对象数组,您处理的不是实际对象的数组,而是对象链接的数组。因此,通过将数组中的位置设置为空或将位置设置为新对象,旧对象将被删除。所以:

ObjArray[4] = null;

并且

ObjArray[4] = new Object;

会删除旧对象。

编辑:我想我忘了提一下,内存中的单个对象可以有多个链接,直到所有这些链接都被删除后,该对象才能被垃圾收集删除。

Object obj = new Object();
ObjArray[4] = obj;
obj=null;

在上面的情况下,当 obj 设置为 null 时,最初创建的对象不会被删除,这是因为 ObjArray 中仍然存在指向它的链接。在对象消失之前,您必须将 obj 和 ObjArray[4] 设置为 null。

It helps to realize that you aren't dealing with actual objects, you are dealing with pointers or references to objects in memory. So:

Object obj = new Object();

is not an object but a link to an object in memory. Removing the link by setting it to null or replacing it with a link to another object, the old object is deleted...ish. When garbage collection runs, it is deleted. So:

obj = null;

and

obj = new Object();

will essentially delete the old object. The same applies to an array of objects, you aren't dealing with an array of actual objects but an array of links to objects. So by setting a position in an array to null or setting a position to a new object, the old object is deleted. So:

ObjArray[4] = null;

and

ObjArray[4] = new Object;

Will delete the old object.

Edit: Something I guess I forgot to mention, A single object in memory can have multiple links to it and it is not until all of those links are remove that the object is in a position to be deleted by garbage collection.

Object obj = new Object();
ObjArray[4] = obj;
obj=null;

In the above case, the object that was originally created will not be deleted when obj is set to null, this is because a link to it still exists in the ObjArray. You have to set both obj and ObjArray[4] to null before the object is gone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文