Question

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 "antherList" 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? Thank you very much.

Was it helpful?

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:

  • Return the sublist:

    List myFunction(int x, int y) { return anotherList.subList(....);}
    

    I know that this way gets rids of your boolean return value.

  • Create a structure that holds the pointer to the List and.

    class Reference <T>
    {
        public T ref;
    }
    
    boolean myFunction(int x, int y, Reference<List> listRef)
    {
          listRef.ref = anotherList.subList(....);
          return true;
    }
    
  • Create a structure that holds all the output you want the method to return:

    class MyFunctionOutput
    {
         List list;
         boolean b;
    }
    
    MyFunctionOutput myFunction(int x, int y)
    {
         MyFunctionOutput out = new MyFunctionOutput();
         out.list = anotherList.subList(....);
         out.b = true;
         return out;
    }
    
  • Or the most easy way: pass an initialized List instead of null and let the function add the sublist, like Attila suggested.

OTHER TIPS

You cannot pass an out parameter in Java. You will either need to state the return type as the List or change the contents of the passed list:

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

Note: out-parameter means that you change the value of the variable containing the value passed to the function. In your case it would mean returning a different List object. Instead, you are returning the same List object, but with changed content.

Think of the parameter as a local copy of the passed value: any changes that replace the whole object will be lost upon the function returning. If you just change its content/state, that change will remain as you are accessing the same object as you have passed in (the local copy is made of the reference pointing to the same object)

Note: it might have been for demonstration purposes, but you should use generics to specify the object contained by the list: e.g. List<String> instead of the raw List type

References in Java are passed by value. So you can't specify an out parameter.

What you can do (however) is to mutate an object passed in. So in your example above, you can pass in an empty List, and populate it within your called method.

i.e. in the above:

myList.addAll(...);

You would likely want to assert as a precondition that the passed list is empty.

I would perhaps suggest that this isn't a very common pattern in the Java world. Because out parameters don't exist, it's not expected behaviour that arguments passed in as parameters would change, and you may be better off creating a suitable object type to return.

The myList variable is local to that function. References are passed by value, not by pointer in the Java world. As soon as you exit, it is removed from the context stack. A possible option is to not return a boolean but return the list itself. Then you can test the list for null to see if your function worked or not.

Another option is to actually create your list, then pass it to the function (non-null). Then your function need not create a new list, but simply add values to it.

A short answer- just return the new list as return value of the function:

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

the long answer is that java copy the pointer of your object when you call a function, that mean that if you assign a new object to the method param, it wouldn't affect the original variable. so you must save the param reference if you want to change the object value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top