ArrayList实际存储了什么 - 引用对象或实际对象?

时间:2013-09-17 16:58:04

标签: java arraylist

假设我的代码是这样的:

ArrayList list = new ArrayList();
Student s = new Student();    // creating object of Student class

myList.add(s);      // Here am confused ...

/* myList contains just the reference variable to the Student object, OR

   myList contains the actual Student object (memory allocation for name, rollNo etc)  ??   
*/

使用add():

将对象添加到ArrayList时

ArrayList是“对象的引用”或其“实际对象”列表的列表

9 个答案:

答案 0 :(得分:14)

在Java中,你永远不会传递实际的对象。您总是在处理引用,它实际上只是存储对象的内存中的一个地址。

由于您从不使用实际对象,因此ArrayLists包含对存储在其他位置(内存中称为堆的位置)的对象的引用数组。

答案 1 :(得分:4)

ArrayList存储对象的引用。我建议你使用ArrayList的通用版本。 您的声明将是:

ArrayList <Student> list = new ArrayList<>();

您将从编译时的类型检查中受益。

另请阅读http://en.m.wikipedia.org/wiki/Object_copy以获取有关对象复制概念的解释以及Java和C ++采用的不同策略。

答案 2 :(得分:4)

对象存储在堆上,而不是存储在arraylist中。正如你所说,arraylist存储对找到对象的位置的引用。

答案 3 :(得分:2)

ArrayList中的对象本身存储在堆上。 ArrayList只提供对这些对象的引用,ArrayList实例也在堆上。

在一天结束时,对象引用只是存储对象的内存中的地址。因此,ArrayList包含对存储在堆(或内存)中的对象的引用数组。

答案 4 :(得分:2)

ArrayList存储对象的引用。

该片段会向您解释

public static void main(String[] args) {

    ArrayList<Dog> a=new ArrayList<TEST.Dog>();
    Dog d=new Dog("AL");

    a.add(d);
    d.setName("AFTER");

    System.out.println(a);
}

在这里,我们将Dog对象独立地更改为列表的一侧,并将其反映到列表中,因此引用将存储在列表中。

答案 5 :(得分:1)

我知道这比必要的更具体,但是Java按值存储了Object的引用。我会解释一下,但这里已有一篇好文章:Java is Pass-by-Value, Dammit!

Scott Stanchfield还在stackoverflow/reference-or-value上添加了一些额外的说明。

答案 6 :(得分:0)

取决于你如何看待它。 但它始终是一个参考。

以下是一个例子:

String niceText = "Hallo array List";
ArrayList<String> list = new ArrayList<String>();
list.add(niceText);
System.out.print(niceText + " = " + list.get(0));
// Output: Hallo array List = Hallo array List

niceText = "Goodby list";
System.out.print(niceText + " = " + list.get(0));
// Output: Goodby list = Goodby list

list.get(0) = "Still in here";
System.out.print(niceText + " = " + list.get(0));
// Output: Still in here = Still in here

list.add("New value");
System.out.print(niceText + " = " + list.get(1));
// Output: Still in here = New value
// But there is no referencing object anymore for index 1, it exist only in the list

但是你可以通过克隆你的对象或以不同的方式将它传递给你的应用程序的其他组件来进一步发展。 但这与数组列表无关,这就是java handels对象实例和可见性的方式。

答案 7 :(得分:0)

        ArrayList<Dog> arrayList = new ArrayList<Dog>();
        Dog dogReference = new Dog("AL");
        arrayList.add(dogReference);

        System.out.println(arrayList.get(0)); //Dog@ObjectRef
        dogReference.setName("AFTER");

        dogReference = new Dog("NEWER");

        // This is still referencing old reference though we have re-initialized
        System.out.println(arrayList.get(0)); //Dog@ObjectRef
        System.out.println(arrayList.get(0).getName());     //AFTER
        System.out.println(dogReference.getName());        //NEWER

最初,我在这个例子之后感到困惑,因为在重新初始化相同的变量arrayList之后仍然指向较旧的变量。 ArrayList使用Object[]来存储引用。

答案 8 :(得分:-1)

我会咬人的。这显然是一个关于Java中对象引用的更大问题的问题。也许有必要问为什么你需要知道你的对象是通过引用还是按值存储。

这可能是一个更有趣的问题,因为Java中的大多数内容,除了原语和其他一些内容,都是通过引用存储的。为了能够解决任何问题,您需要考虑为什么您的思维过程需要知道,如果某些内容是通过引用或价值更有可能的话提示你不得不问这个问题。

好奇。

相关问题