javalistfunction-calloutput-parameter

List as output parameter in Java


I am trying to write a Java function which has List object as output parameter.

boolean myFunction(int x, in y, List myList)
{
    /* ...Do things... */
    myList=anotherList.subList(fromIndex, toIndex);
    return true;
}

Before that I call the function I declare myList as follow:

List myList=null;

Then I call the function

myFunction(x,y,myList)

But when I try to manipulate myList, I find that myList is still null.

I am sure the variable anotherList in my function code is not null and I am sure that the subList function return a non-empty List.

What is the reason, and how can pass a List as output parameter in a Java function?


Solution

  • Java always uses pass by value. This means that manipulating a passed variable won't affect the variable that was passed by the caller.

    In order to solve your problem there are some possibilities: