简体   繁体   English

get方法返回值

[英]get method return value

i run following codes in eclipse: 我在eclipse中运行以下代码:

ArrayList<StringBuilder> list = new ArrayList<StringBuilder>();
ArrayList<Integer> alist = new ArrayList<Integer>();

// add some elements ti list
list.add(new StringBuilder("hello"));
list.add(new StringBuilder("2"));
list.add(new StringBuilder("hi"));
list.add(new StringBuilder("this"));

// add some elements to alist
alist.add(4);
alist.add(9);


//get method
StringBuilder a = list.get(3);
a.append(" is a good day");
int b = alist.get(1);
b = 7;

// print the list
System.out.println("LinkedList:" + list);
System.out.println("ArrayList:" + alist);

and result is here 结果就在这里

LinkedList:[hello, 2, hi, this is a good day]
ArrayList:[4, 9]

It looks like get method returns a shallow copy of list element (in the case of StringBuilder) to the a, but returns a deep copy (in the case of integer) to the b! 看起来get方法返回一个list元素的浅表副本(在StringBuilder的情况下)到a,但是返回一个深拷贝(在整数的情况下)到b! why it happened? 为什么会这样? Do the get method return a deep or shallow copy of list's elements? get方法是否返回列表元素的深层或浅层副本?

get returns a reference to the List element, not a copy (neither deep nor shallow). get返回对List元素的引用,而不是副本(既不深也不浅)。

In the first snippet you mutate the object referenced by variable a , so the List is also affected: 在第一个片段中,您改变变量a引用的对象,因此List也会受到影响:

StringBuilder a = list.get(3);
a.append(" is a good day");

In the second snippet you assign a new value to the variable b , which doesn't affect the List : 在第二个片段中,您为变量b分配一个新值,这不会影响List

int b = alist.get(1);
b = 7;

In order for your first snippet to behave as the second, you should write: 为了使您的第一个代码段成为第二个代码段,您应该写:

StringBuilder a = list.get(3);
a = new StringBuilder(" is a good day");

and the List won't be affected. 并且List不会受到影响。

On the other way, you can't make the second snippet behave as the first. 另一方面,您无法使第二个代码段表现为第一个。 Even if you assigned the List element to an Integer variable, you can't call any method that would mutate it, since Integer is immutable. 即使您将List元素分配给Integer变量,也不能调用任何会使其变异的方法,因为Integer是不可变的。

In the case of StringBuilder a = list.get(3); StringBuilder a = list.get(3);的情况下StringBuilder a = list.get(3); you get a reference to element at index 3 assigned to variable a , modifying using a will affect element at index 3 . 你得到一个对索引为3元素的引用赋值给变量a ,使用a将影响索引3元素进行修改。 where as int b = alist.get(1); 其中int b = alist.get(1); you get a copy of element at 1 assigned to variable b so modifying it will not affect element at 1 . 你在元素的副本1赋给变量b如此调整不会影响在元素1 there is not connection between b and element at 1 after assignment . 没有之间的连接b在与元件1分配之后。

   StringBuilder a = list.get(3);
    a.append(" is a good day");
    int b = alist.get(1);
    b = 7;
StringBuilder a = list.get(3);
a.append(" is a good day");
int b = alist.get(1);
b = 7;

Indeed you create a shallow copy of your list element in int b =... . 实际上,您在int b =...创建了list元素的浅表副本。 As your List contains Objects of Type Integer you make a new assignment to a primitive integer here, while with the StringBuilder you work on exactly the same object. 由于List包含Integer类型的对象,因此您在此处对原始整数进行新赋值,而使用StringBuilder则可以使用完全相同的对象。

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

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