简体   繁体   中英

Do arraylist needs to be specified as return type in method after modification?

I would like to know what is the best practice to return 'updated' ArrayList ?

For example, if I am adding in a new element, it seems that whether if I did or did not specify the return type (see the addNewA() and addNewB() ), the ArrayList in my main() method will still be 'updated' n.netheless.

Should I or should I not specify the return type? Currently in my client program, most of the methods, I have specified it as void (no return type) and while the overall program still does works as intended, thought I would like to get this clarified and make the necessary changes if necessary.

public class MyClient
{
    public static ArrayList<Person> addNewA(ArrayList<Person> myArray)
    {
        Person jack = new Person("jack", 24);
        myArray.add(jack);
        return myArray;
    }

    public static void addNewB(ArrayList<Person> myArray)
    {
        Person ben= new Person("ben", 19);
        myArray.add(ben);
    }

    public static void main(String[] args)
    {
        ArrayList<Person> personArray= new ArrayList();
        addNewA(personArray); // will return me an array size of 1
        addNewB(personArray); // will return me an array size of 2
    }
}

In java (and most high level strict type languages) Object s are passed by reference and primitives passed by value .

When using the new keyword you create an object.

While primitives (like int , char , double ect) are passed by value (meaning that a copy of the value of the variable will be sent to the invoked function), Object types are passed by reference, meaning that the actual original object is passed to the function.

To sum up - since you are using object here ( ArrayList ), you don't need a return type since the original object is changing.

In a case like this, you should not return the list and should make your method void .

Different languages have different conventions, but one of Java's is that methods that operate by modifying their arguments (or the object they're called on) should not return values. Returning a value implies to someone using your code that a different object is being returned, since otherwise there is no use in returning an object the caller already has 1 . A method that is void , on the other hand, couldn't possibly be returning a copied-and-extended list, so it's very clear that it's intended to operate by modifying the list that you give it in the first place.

(By the way, you should also just use List<Person> , and you should pay attention to the warning you get about using new ArrayList() instead of new ArrayList<>() .)


1 There is a specific exception to this, called the fluent builder pattern, but it's not easily confused with general code like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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