Home > Blockchain >  List not being updated inside method [duplicate]
List not being updated inside method [duplicate]

Time:09-22

I'm trying to modify a list as a parameter, but I'm not getting the expected result. I thought that all parameters in Java were passed as reference, but in this case it seems it's passed as value. How can I get the filled list in the main method (without change fillList firm)?

Code:

import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        List<String> list=null;
        fillList(list);
        System.out.println("OUTSIDE");
        for(String elem: list){
            System.out.println(elem);
        }
    }
    
    public static void fillList(List<String> lista){
        lista = new ArrayList<>();
        lista.add("inside");
        System.out.println("INSIDE");
        for(String elem: lista){
            System.out.println(elem);
        }
    }
}

Result:

INSIDE
inside
OUTSIDE
Exception in thread "main" java.lang.NullPointerException
    at Main.main(Main.java:11)

[Program exited with exit code 1]

CodePudding user response:

try this:

    import java.util.*;
    import java.lang.*;
    import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        List<String> list = fillList();
        System.out.println("OUTSIDE");
        for(String elem: list){
            System.out.println(elem);
        }
    }
    
    public static void fillList(){
        List<String> lista = new ArrayList<>();
        lista.add("inside");
        System.out.println("INSIDE");
        for(String elem: lista){
            System.out.println(elem);
        }
     return lista ;
    }
}

for complete explanation see is-java-pass-by-reference-or-pass-by-value

CodePudding user response:

Since you pass the list by reference, and not by value, any change made to it inside the method will affect the original list in the main method. With that being said, your line lista = new ArrayList() actually changes said reference. Hence, any change made to your list past that line won't be saved once the method terminates. To solve your problem, I suggest this code -

import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        List<String> list = fillList(list);
        System.out.println("OUTSIDE");
        for(String elem: list){
            System.out.println(elem);
        }
    }
    
    public static ArrayList<String> fillList(List<String> lista){
        lista = new ArrayList<String>();
        lista.add("inside");
        System.out.println("INSIDE");
        for(String elem: lista){
            System.out.println(elem);
        }
        return lista;
    }
}

CodePudding user response:

In your code the list which is being passed to fillList method is null. And you are initializing it inside the fillList method. Java is passByReference but here in your code there is not reference to a list object that is being passed (it's null). And when you assign a new Object (here it's new ArrayList) to it, the reference is updated. That's why changes are not being reflected in the calling method. And list reference is calling method and in called method are different.

If you want to retain the list value you need to pass an initialized list and avoid initializing it in called method:

class Main {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        fillList(list);
        System.out.println("OUTSIDE");
        for(String elem: list){
            System.out.println(elem);
        }
    }
    
    public static void fillList(List<String> lista){
        lista.add("inside");
        System.out.println("INSIDE");
        for(String elem: lista){
            System.out.println(elem);
        }
    }
}

Output :

INSIDE
inside
OUTSIDE
inside

  • Related