在数组中的元素对象或引用它自身的参考? [英] Is an element in an array a reference to an object or a reference it self?

查看:93
本文介绍了在数组中的元素对象或引用它自身的参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有AnObjects的数组,我这样做:

  AnObject的OLDobject = arrayOfObjects [validIndex]
arrayOfObjects [validIndex] =新AnObject(oldObject.getVariableForContruction);
oldObject.terminate();

请问的 arrayOfObjects [validIndex] 被终止,还是原来的的OLDobject 被终止?<新内容/ p>

在换句话说:是否的OLDobject包含一个AnObject参考或它包含一个参考的引用一个AnObject


解决方案

这code是一个有点混乱。你的对象数组必须有一个参考 AnObject

  AnObject的OLDobject = arrayOfObjects [validIndex]

您分配一个参照的OLDobject 在这里。

  arrayOfObjects [validIndex] =新AnObject(oldObject.getVariableForContruction);

现在你定在 validIndex 数组引用指向 AnObject 的新实例。它不再指向的OLDobject

  oldObject.terminate();

这是一个对象的引用在堆上。

的OLDobject 指的是在堆上实例;在数组指向你在堆上创建新实例的引用。

如果的OLDobject 终止(这意味着什么),它无关紧要到您创建的新实例。该阵列仍然指向到新的实例。

If i have an array of AnObjects and i do this:

AnObject oldObject = arrayOfObjects[validIndex];
arrayOfObjects[validIndex] = new AnObject(oldObject.getVariableForContruction);
oldObject.terminate();

Does the new content of arrayOfObjects[validIndex] get terminated, or does the original oldObject get terminated?

In other words: Does oldObject contain a reference to an AnObject or does it contain a reference to a reference to an AnObject?

解决方案

This code is a bit confusing. Your array of objects must have a reference to AnObject.

AnObject oldObject = arrayOfObjects[validIndex];

You assign that reference to oldObject here.

arrayOfObjects[validIndex] = new AnObject(oldObject.getVariableForContruction);

Now you set the array reference at validIndex to point to a new instance of AnObject. It no longer points to oldObject.

oldObject.terminate();

It's a reference to an object on the heap.

oldObject refers to an instance on the heap; the reference in the array points to the new instance that you created on the heap.

If oldObject is terminated (whatever that means), it's immaterial to the new instance you created. The array still points to the new instance.

这篇关于在数组中的元素对象或引用它自身的参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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