简体   繁体   English

将对象存储在对象数组中

[英]Storing objects in array of objects

Basically i want to add students to a class list. 基本上我想将学生添加到班级列表中。 Assuming i have the following code 假设我有以下代码

public class ClassList {

    //Constructor methods...


    private Student [] studList = new Student [20];

    public boolean addStudent (Student newStudent)
    {

        studList[14] = newStudent;
    }
}

Does studList[14] add a reference to newStudent object or copies that object into the studList[14] student object? studList [14]是否添加对newStudent对象的引用或将该对象复制到studList [14]学生对象中?

As far as i understand newStudent object will get deleted when the method addStudent() is called a second time. 据我所知,当第二次调用addStudent()方法时,newStudent对象将被删除。 So studList[14] will point to null then? 那么studList [14]会指向null呢? What if i want studList[14] to persist throughout the code execution? 如果我希望studList [14]在整个代码执行过程中持续存在怎么办?

Sorry if it is hard to understand, i do not know how to explain my query easier... 对不起,如果很难理解,我不知道如何更容易解释我的查询...

There's one fundamental rule in Java that you have to wrap your head around: 在Java中有一个基本规则,你必须解决这个问题:

The only way you can access an object is via its reference. 您可以访问对象的唯一方法是通过它的引用。 And the only values that variables can hold are references (*) . 变量可以容纳的唯一引用 (*) That's true for local variables, parameters, instance fields and static fields: they all are the same in this respect. 对于局部变量,参数,实例字段和静态字段都是如此:它们在这方面都是相同的。

The object itself is never "contained" in a variable. 对象本身永远不会“包含”在变量中。

This directly leads to the answer to your first question: 这直接导致您的第一个问题的答案:

The reference is copied into the array (as an array can only hold references (*) , never objects ). 引用被复制到数组中(因为数组只能包含引用(*) ,从不包含对象 )。

This also mean that " newStudent being deleted" is not actually a big deal: it's just another reference to the same object, and if it goes away nothing much happens. 这也意味着“ newStudent被删除”实际上并不是什么大问题:它只是对同一个对象的另一个引用,如果它消失了,就不会发生什么。

Now, if all references to a given object are removed (or no longer reachable), then the object itself becomes eligible for garbage collection, but that's not a bad thing, because you could not access it anymore anyway. 现在,如果删除了所有对给定对象的引用(或者不再可访问),那么对象本身就有资格进行垃圾回收,但这并不是坏事,因为无论如何你都无法访问它。

(*) ... or primitive values, but we'll ignore those for this dicussion. (*)......或原始值,但我们会忽略这些讨论。

The assignment doesn't copy the object. 赋值不会复制对象。 It just adds a reference to the object into the array. 它只是将对象的引用添加到数组中。

An object gets deleted by the garbage collector after there are no more references to it anywhere. 垃圾收集器在任何地方都没有对它进行更多引用后,对象将被删除。 You don't need to worry too much about this process, because it's kind of invisible most of the time; 你不需要过多担心这个过程,因为它在大多数时候都是看不见的; and once there are no references to an object, you couldn't have used it anyway. 一旦没有对象的引用,你无论如何都无法使用它。

Perhaps you want to be able to pass in an int to your method, to tell it which entry in the array to set, instead of always setting entry number 14? 也许您希望能够将int传递给您的方法,告诉它要设置数组中的哪个条目,而不是始终设置条目号14?

You should have a look at the JLS about types, values and variables : 您应该查看有关类型,值和变量JLS

The values of a reference type are references to objects. 引用类型的值是对对象的引用。

If there is no remaining reference (aside weak ones) to an object it will be garbage-collected out of the heap. 如果没有剩余的引用(除了弱引用),它将被从堆中垃圾收集。

If you call two times: addStudent(new Student()); 如果你打两次电话: addStudent(new Student()); the first Student object created is qualified for GC since you have no other reference variable "pointing" to the object. 创建的第一个Student对象符合GC的条件,因为没有其他引用变量“指向”该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM