Java ArrayList 实际存储什么 - 对对象或实际对象的引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18855639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does ArrayList actually stores - References to objects or actual objects?
提问by Mustafa
Suppose my code goes like this:
假设我的代码是这样的:
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) ??
*/
In Short when adding objects to ArrayList using add():
简而言之,使用 add() 将对象添加到 ArrayList 时:
ArrayList is a list of "References to objects" or its a list of "actual objects" ???
ArrayList 是“对象引用”列表或其“实际对象”列表???
回答by Tarik
ArrayList stores references to objects. I would advise you to use the generic version of ArrayList. Your declaration would be:
ArrayList 存储对对象的引用。我建议您使用 ArrayList 的通用版本。您的声明将是:
ArrayList <Student> list = new ArrayList<>();
You would benefit from type checking at compile time.
您将从编译时的类型检查中受益。
Also read http://en.m.wikipedia.org/wiki/Object_copyfor explanations about object copy concepts and the different strategies adopted by Java and C++.
另请阅读http://en.m.wikipedia.org/wiki/Object_copy以了解有关对象复制概念以及 Java 和 C++ 采用的不同策略的说明。
回答by Zavior
The objects are stored on the heap, not 'inside' the arraylist. The arraylist stores references to where there objects are found, as you say.
对象存储在堆上,而不是存储在数组列表的“内部”。如您所说,arraylist 存储对找到对象的位置的引用。
回答by blackpanther
Objects within the ArrayList
themselves are stored on the heap. The ArrayList
simply provides references to those objects and the ArrayList
instance is also on the heap.
ArrayList
自身内部的对象存储在堆上。在ArrayList
简单地提供引用这些对象和ArrayList
实例也是在堆上。
Object references, at the end of the day, are simply addresses to locations within memory where the object is stored. Hence, the ArrayList contains arrays of references to objects stored on the heap (or in memory).
归根结底,对象引用只是指向存储对象的内存中的位置的地址。因此,ArrayList 包含对存储在堆(或内存)上的对象的引用数组。
回答by Johan Henriksson
In Java, you never pass around actual objects. You are always dealing with a reference, which is essentially just an address to a location within memory where your object is stored.
在 Java 中,您永远不会传递实际对象。你总是在处理一个引用,它本质上只是一个地址,指向内存中存储你的对象的位置。
Since you never work with actual objects, ArrayLists contains arrays of references to objects stored somewhere else (a place in memory called the heap).
由于您从不使用实际对象,因此 ArrayLists 包含对存储在其他地方(内存中称为堆的地方)的对象的引用数组。
回答by Rene M.
Depends on how you see it. But its always a reference.
取决于你如何看待它。但它始终是一个参考。
Here is an example:
下面是一个例子:
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
But you can go much further, by cloning your object or pass it in different ways to other components of your app. But this has nothing to do with the array list, this is how java handels object instances and there visibility.
但是你可以更进一步,通过克隆你的对象或以不同的方式将它传递给你应用程序的其他组件。但这与数组列表无关,这就是 java 处理对象实例和可见性的方式。
回答by kalakanhu
ArrayList
stores the references of objects.
ArrayList
存储对象的引用。
The snippet will explain to you
片段将向您解释
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);
}
Here we are changing the Dog
object independently out side of the list and it is getting reflected to the List, hence a reference is being stored in the list.
在这里,我们Dog
在列表之外独立地更改对象,并且它被反映到列表中,因此引用被存储在列表中。
回答by subdigit
I'll bite. This is obviously a question resulting from a larger issue about object references in Java. Perhaps it'll help to ask whyyou would need to know if your object is being stored by reference or by value.
我会咬的。这显然是一个关于 Java 中对象引用的更大问题导致的问题。询问为什么需要知道对象是按引用存储还是按值存储可能会有所帮助。
That's probably the more interesting question to answer as most everything in Java, except primitives and a few others, are stored by reference. To be at a point where you're needing to think about why your thought process needs to knowif something is by reference or value is more likely the point where you'll be able to resolve any issues that prompted you to have to ask this question.
这可能是一个更有趣的问题,因为 Java 中的大多数内容,除了原语和其他一些,都是通过引用存储的。当您需要思考为什么您的思维过程需要知道某事是通过引用还是通过价值时,您更有可能解决任何促使您提出这个问题的问题题。
Just curious.
只是好奇。
回答by Funny Geeks
I know this is more specific than necessary, but Java stores an Object's reference by value. I would explain it, but there is already a good article here: Java is Pass-by-Value, Dammit!.
我知道这比必要的更具体,但 Java 按值存储对象的引用。我会解释一下,但这里已经有一篇很好的文章:Java is Pass-by-Value,该死!.
Scott Stanchfieldalso added some extra clarification on stackoverflow/reference-or-value.
Scott Stanchfield还对stackoverflow/reference-or-value添加了一些额外的说明。
回答by Ravi Parekh
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
Initially, I was confused after this example as after re-initializing same variable arrayList
still point to the older one. ArrayList
uses Object[]
to store the references.
最初,我在这个例子之后感到困惑,因为在重新初始化相同的变量后arrayList
仍然指向旧的变量。ArrayList
用于Object[]
存储引用。