简体   繁体   English

将两个arrayList引用到相同的对象

[英]Reference of two arrayList to same objects

I have this code. 我有这个代码。 But I don't know how to explain the result: 但我不知道如何解释结果:

ArrayList<String> first = new ArrayList<String>();
        first.add("1");
        first.add("2");
        first.add("3");

        ArrayList<String> second = new ArrayList<String>();
        second = first;
        System.out.println("before modified:"+second.size());
        second.clear();
        System.out.println("after modified:");
        System.out.println("   First:"+first.size());
        System.out.println("   Second:"+second.size());

The result will be: 3 / 0 /0 结果将是:3/0/0

The problem I don't know is: when you assign first = second; 我不知道的问题是:当你分配first = second; so, both first and second array will point to same object (1,2 and 3). 因此,第一个和第二个数组都将指向同一个对象(1,2和3)。 after you clear all elements on second array, so all reference between second array and these objects will loose (no problem here). clear第二个数组上的所有元素后,第二个数组和这些对象之间的所有引用都将松散(此处没有问题)。

The thing I don't know is: but these objects (1,2 and 3) still hold reference to first array. 我不知道的是:但是这些对象(1,2和3)仍然引用第一个数组。 Why first array's size is 0. 为什么第一个数组的大小为0。

Please explain for me. 请解释一下。

Thanks :) 谢谢 :)

By assigning second = first , there is only one arraylist with two references. 通过指定second = first ,只有一个具有两个引用的arraylist。 The references are the same. 参考文献是相同的。 So, when call clear using one of the two references ( first or second ), clear will be performed on the referenced arraylist. 因此,当使用两个引用之一( firstsecond )调用clear时,将对引用的arraylist执行clear。

This is something else than you first thought. 这是你最初想到的其他事情。 It's not so that assigning the second = first all the references of the strings you added to the first one, will be copied into a new ArrayList object, that would be magic (in Java). 不是这样,将second = first分配给你添加到第一个字符串的所有字符串的引用都将被复制到一个新的ArrayList对象中,这将是魔术(在Java中)。

When you do first = second your ArrayList items will point to the same memory locations. 当您执行first = second您的ArrayList项将指向相同的内存位置。 Doing a .clear will remove the elements to which the ArrayList is pointing to. 执行.clear将删除ArrayList指向的元素。 This will have repercussions on the other ArrayList. 这将对其他ArrayList产生影响。

If you just want to copy the elements of ArrayList1 to ArrayList2 , you could do something like so: ArrayList<String> second = new ArrayList<String>(first); 如果你只想将ArrayList1的元素复制到ArrayList2 ,你可以这样做: ArrayList<String> second = new ArrayList<String>(first);

but these objects (1,2 and 3) still hold reference to first array. 但是这些对象(1,2和3)仍然引用第一个数组。 Why first array's size is 0. 为什么第一个数组的大小为0。

ArrayList<String> second = new ArrayList<String>();
second = first;

is the same as writing 和写作一样

ArrayList<String> second = first;

You have made second reference point to the first arraylist,it is not using a new arraylist. 你已经为第一个arraylist提出了第二个参考点,它没有使用新的arraylist。 So when you call clear it clears the "first" arraylist created - you have two references pointing to one arraylist. 所以当你打电话给它时,它会清除所创建的“第一个”arraylist - 你有两个引用指向一个arraylist。

When you assign one ArrayList to two variable and modify any one of them, this will reflect in both.So operation performed in any of one variable also reflect in second one. 当您将一个ArrayList分配给两个变量并修改它们中的任何一个时,这将反映在两者中。所以在任何一个变量中执行的操作也反映在第二个变量中。 (Single object referenced by two variable). (由两个变量引用的单个对象)。

In Java a variable (except primitives) is always a reference (which has the start address of object) to an object only, Reference is never an object in itself. 在Java中,变量(除了基元)始终是对象的引用(它具有对象的起始地址),Reference本身不是对象。

For example 例如

second = first; 第二个=第一个

is assigning a reference, so that first and second now refering to the same object. 正在分配一个引用,所以第一个和第二个现在引用同一个对象。 Objects are not copied, neither in assignments, nor in argument passing (what is copied/assigned is the reference). 对象不会在赋值中传递,也不会在参数传递中复制(复制/赋值的是引用)。

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

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