简体   繁体   English

返回两个数组列表或使用两种不同的方法

[英]Return Two Arraylists or Use Two Different Methods

If I have a method 如果我有方法

 public ArrayList<String> necessaryChanges(Set<Object> setToCheck ) {

 //checks to see whether each element of set is correct by comparing it to something
 // and if it needs to be changed its added to an arraylist to be returned to the user

     }

I need to return a list with necessary changes which is fine. 我需要返回一个列表,其中包含必要的更改,这很好。

But I also want to modify setToCheck with the changes. 但是我也想通过更改来修改setToCheck。 I know you cant return two objects in a method. 我知道您不能在方法中返回两个对象。 What I am asking is what is the most efficient way of doing this. 我要问的是什么是最有效的方法。

I know I can create another method in the main class and call it to change the set, but it seems really inefficient. 我知道我可以在主类中创建另一个方法并调用它来更改集合,但这似乎效率很低。

Is there a better way of doing this. 有没有更好的办法做到这一点。

Thanks 谢谢

If you modify the setToCheck object inside the necessaryChanges method, the changes will be visible also outside of that method, since in Java everything (primitive types excluded, but it's not your case) is passed as reference. 如果您在necessaryChangessetToCheck方法中修改setToCheck对象,则更改也将在该方法之外可见,因为在Java中,所有内容(原始类型setToCheck排除,但并非您的情况)均作为引用传递。

So basically you don't need to return the setToCheck object: you can simply still use it after the method call: it is always the same object (inside and outside the method), therefor it will contains the new changes. 因此,基本上您不需要返回 setToCheck对象:您可以在方法调用后仍然简单地使用它:它始终是同一对象(方法内部和外部),因此它将包含新的更改。

By your problem description, you don't need to return two objects. 根据问题描述,您不需要return两个对象。 You can just create and return an ArrayList and make alterations to setToCheck in-place. 您可以只创建并return一个ArrayList并就地更改setToCheck Since setToCheck is an object reference, any changes made to the object inside the method will be visible outside. 由于setToCheck是对象引用,因此对方法内部对象的任何更改都将在外部可见。

As others have pointed out, Java is pass by reference. 正如其他人指出的那样,Java是通过引用传递的。 So, if you change an object in setToCheck, it will be changed in memory and therefore no need to do anything other than change your object reference. 因此,如果您在setToCheck中更改对象,则它将在内存中更改,因此除了更改对象引用外,无需执行任何其他操作。 For instance... 例如...

    public List<String> neccessaryChanges(Set<Object> setToCheck) {
        List<String> changeList = new ArrayList<String>();
        Iterator<Object> iterator = setToCheck.iterator();
        while(iterator.hasNext()) {
            Object objectToCheck = iterator.next();
            if(objectMustChange) {
                //once you change the object here, it will change 
                //in setToCheck since objectToCheck now equals something
                //different and setToCheck still has reference to 
                //objectToCheck
                objectToCheck = new String();
                changeList.add((String) objectToCheck);
            }
        }
        return changeList;
    }

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

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