如何在Java中正确删除数组 [英] How to delete an array properly in Java

查看:74
本文介绍了如何在Java中正确删除数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java已有4天的年龄,从我搜索过的教程中,讲师将大量精力用于解释如何分配二维数组(例如):

I'm 4 days old in Java and from the tutorials I've searched, the instructors focus a lot of effort in explaining how to allocate a two dimensional array (e.g.) as such:

Foo[][] fooArray = new Foo[2][3];

...但是我没有找到任何说明如何删除它们的东西.

... but I've not found any that explains how to delete them.

从内存的角度来看,变量fooArray将指向堆中的一块内存,其中有2个元素.每个元素也指向堆中的另一个块,该块具有3个元素.

From what is going on memory-wise, the variable fooArray will point to a block of memory in the heap, in which there are 2 elements. Each of the elements points to another block in the heap as well, which have 3 elements.

话虽这么说,我可以只引用第一个元素块,然后垃圾收集器将完成这项工作吗?

That being said, could I just reference the first block of elements and the garbage collector will do the job?

Foo[1] = null;Foo[2] = null;

还是我必须使每个实例化的Foo元素都为空?

Or do I have to null each of the instantiated Foo elements?

Foo[1][1] = null; Foo[1][2] = null; Foo[1][3] = null; ...

推荐答案

说明

您不能在Java中显式删除某些内容.这样做是垃圾收集者的工作.它将删除任何人不再使用的任何内容.所以

Explanation

You can not explicitly delete something in Java. It is the garbage collectors job to do that. It will delete anything which is not used anymore by anyone. So either

  1. 让变量超出范围或
  2. 分配null
  3. 或其他任何实例.

然后不再引用该数组实例(及其子数组),并且垃圾回收器最终将其删除.

Then the array instance (as well as its subarrays) is not referenced anymore and the garbage collector will delete it eventually.

要了解为什么重新分配外部数组足以删除内部数组,您需要了解如何引用它们.同样,垃圾收集器可以删除 无法访问 .因此,让我们看一下这样的数组:

To understand why re-assigning the outer array is enough to also delete the inner arrays, you need to understand how they are referenced. Again, the garbage collector can delete anything which is unreachable. So let's take a look at an array such as:

int[][] outer = {{1, 2}, {3, 4}, {5, 6}};

我们有4个数组实例.一种是int[][]类型,三种是int[]类型.另外,我们有一个变量outer.实例引用如下:

We have 4 array instances. One is of type int[][] and three of type int[]. Also, we have one variable outer. The instances are referenced as follows:

                       ___> {1, 2}
                      |
outer  --> int[][] ---|---> {3, 4}
                      |
                      |___> {5, 6}

因此,通过删除outer,再也没有人引用int[][].垃圾收集器现在可以将其删除.但这也会删除对内部数组的所有引用,因此垃圾收集器现在也可以删除它们.

So by deleting outer, nobody references int[][] anymore. The garbage collector can now delete it. But that also removes all references to the inner arrays, so the garbage collector can now also delete them.

现在假定您将通过另一个变量引用内部数组之一:

Now assume that you would reference one of the inner arrays by another variable:

int[][] outer = {{1, 2}, {3, 4}, {5, 6}};
int[] thirdInner = outer[2];
other = null; // remove the reference

现在的情况

outer  --> null

                       ___> {1, 2}
                      |
           int[][] ---|---> {3, 4}
                      |
                      |______> {5, 6}
                          |
thirdInner _______________|

因此,垃圾收集器现在将删除外部数组int[][],这还将删除对第一个和第二个内部数组的所有引用.但是第三个仍然被thirdInner引用,因此在垃圾回收之后,我们有:

So the garbage collector will now delete the outer array int[][], which also removes all references to the first and second inner array. But the third is still referenced by thirdInner, so after garbage collection we have:

outer       --> null
thirdInner  --> {5, 6}

这篇关于如何在Java中正确删除数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆