简体   繁体   English

ArrayLists 未按预期工作

[英]ArrayLists not working as expected

import java.util.ArrayList;

public class bugs 
{
    public ArrayList<Integer> elements1;
    public ArrayList<Integer> elements2;

    public bugs(ArrayList<Integer> args)
    {
        elements1 = args;
        elements2 = args;
    }

    public void change(int index, int value)
    {
        elements1.set(index, value);
    }

    public void reset()
    {
        elements1 = elements2;
    }

    public static void main(String[] a)
    {
        ArrayList<Integer> stuff = new ArrayList<Integer>();
        stuff.add(1);
        stuff.add(1);
        stuff.add(1);
        stuff.add(1);

        bugs b = new bugs(stuff);
        b.change(2, 999);
        b.reset();

        System.out.println(b.elements2);
    }
}

This outputs:这输出:

[1, 1, 999, 1] [1, 1, 999, 1]

The second arraylist elements2 is there to reset the arraylist elements1 to its original position.第二个arraylist elements2用于将arraylist elements1重置为其原始位置。 However, for some reason elements1 is being copied to elements2 , printing但是,由于某种原因elements1被复制到elements2 ,打印

[1, 1, 999, 1] [1, 1, 999, 1]

and not并不是

[1, 1, 1, 1] [1, 1, 1, 1]

You are passing the same ArrayList reference to both variables.您将相同的 ArrayList 引用传递给两个变量。

What you meant to do is:你的意思是:

public bugs(ArrayList<Integer> args)
{
    elements1 = new ArrayList<Integer>(args);
    elements2 = new ArrayList<Integer>(args);
}

EDIT:编辑:

Note that this is only a temporary fix.请注意,这只是临时修复。 Calling reset() will pass the reference of elements2 to elements1 and then you have the same situation.调用 reset() 会将 elements2 的引用传递给 elements1,然后您会遇到相同的情况。 If you create a new arraylist and you pass another list as argument, you create a new reference with the same contents.如果您创建一个新的数组列表并将另一个列表作为参数传递,您将创建一个具有相同内容的新引用。 This means you must also adjust your reset() method to create a new list and pass elements2 as argument.这意味着您还必须调整 reset() 方法以创建新列表并将元素 2 作为参数传递。

In java you can pass by reference or by value.在 Java 中,您可以按引用或按值传递。 If you are using primitive data types, you are passing by value.如果您使用原始数据类型,则是按值传递。 So:所以:

public void method(int num)
{
   int num1 = num;
   int num2 = num;
}

This method pass num value to num1 and num2 primitive data types.此方法将 num 值传递给 num1 和 num2 原始数据类型。 If you add something to num1 it will not change num2 to the same value.如果您向 num1 添加某些内容,它不会将 num2 更改为相同的值。 But if you are using non primitive data types like ArrayList:但是,如果您使用非原始数据类型,例如 ArrayList:

public bugs(ArrayList<Integer> args)
{
    elements1 = args;
    elements2 = args;
}

You should expect that change in elements1 array will change elements2 array also.您应该期望元素 1 数组中的更改也会更改元素 2 数组。 In this example you are passing the same ArrayList reference to both variables.在本例中,您将相同的 ArrayList 引用传递给两个变量。

The solution for your problem is create copy of your arrays:您的问题的解决方案是创建数组的副本:

public bugs(ArrayList<Integer> args)
{
    elements1 = new ArrayList<>(args);
    elements2 = new ArrayList<>(args);
}

public void reset()
{
    elements1 = new ArrayList<>(elements2);
}

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

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