简体   繁体   English

Java数组列表和对象传递

[英]Java Array List and object passing

I have a list of string in my code. 我的代码中有一个字符串列表。 I am passing that list of string to a function and modifying it.( I am adding/deleting few elements from the list).But i dont want these changes to get reflected in the caller function. 我正在将该字符串列表传递给函数并对其进行修改。(我正在从列表中添加/删除一些元素)。但是我不希望这些更改反映在调用者函数中。 The changes should be reflected only in the callee function. 所做的更改应仅反映在被调用方函数中。 But because objects are passed by reference, I think the changes are getting reflected in both functions. 但是因为对象是通过引用传递的,所以我认为更改已反映在两个函数中。 How can I avoid this. 我如何避免这种情况。 Please help 请帮忙

Inside the method, you can explicitly make a copy of the list: 在方法内部,您可以显式创建列表的副本:

private void method(ArrayList<String> list) {
    ArrayList<String> copy = new ArrayList<String>(list);
    // Rest of the method
}

您必须先复制该对象,然后再将其传递给函数。

在sub方法中使用原始数组列表的新ArrayList。

You can use Clone method. 您可以使用Clone方法。

caller(ArrayList<String> a)
{
    callee((List<String>)a.clone());
}

callee(List<String> aCloned)
{

}

Typically the clone method is not recommended. 通常不建议使用clone方法。 See this clone(): ArrayList.clone() I thought does a shallow copy 看到这个clone():ArrayList.clone()我认为是浅拷贝

However you can try this method 但是你可以尝试这种方法

public void doOperation(List<String> list){
    List<String> duplicateList = new ArrayList(list);
    // add, or delete stuff on duplicateList
}

Here, you use the constructor of the ArrayList to give you a new copy of the list that is passed in. 在这里,您使用ArrayList的构造函数为您提供传入的列表的新副本。

You have to create new list from old list manually using looping and need to pass to that method. 您必须使用循环从旧列表手动创建新列表,并且需要传递给该方法。 ie

   List<String> list=new ArrayList<>();
    list.add("first");
    list.add("last");
    List<String>list2 =new ArrayList<String>();
    for(String value:list)
    {
    list2.add(new String(value));
    }
    System.out.println("list2-->"+list2);

Because new ArrayList<String>(list); 因为new ArrayList<String>(list); is give a new reference of list but the object still have a same reference. 给list的新引用,但是对象仍然具有相同的引用。

Edit: 编辑:

public class ArrayCopy {


    public static void main(String[] args) {

        ArrayCopy arrayCopy=new ArrayCopy();
        arrayCopy.copyData();

    }
    public void copyData()
    {
        List<Test> oldList=new ArrayList<Test>();
        oldList.add(new Test("1",10));
        oldList.add(new Test("2",45));
        List<Test> newList =new ArrayList<Test>(oldList);
        System.out.println("newList-->"+newList);

        /**
         * New Copy of Data
         */
        List<Test> newList1 =new ArrayList<Test>();
        for(Test test:newList)
        {
            newList1.add(copyProperty(test));
        }
        System.out.println("newList-->"+newList1);
    }
    private Test copyProperty(Test test)
    {
        Test newTest=new Test(test.getId(), test.getNumber());
        return newTest;
    }
    class Test{
        String id;
        int number;
        public Test(String id,int number) {
            this.id=id;
            this.number=number;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public int getNumber() {
            return number;
        }
        public void setNumber(int number) {
            this.number = number;
        }
    }
}

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

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