How to initialize a list via method in Java

1 Answer

0 votes
import java.util.*;

public class MyClass {
    public static void main(String[] args) {
        List<String> lst = getValues();
        
        System.out.println(lst); 
    }

    private static List<String> getValues() {
        return new ArrayList<>(Arrays.asList("java", "c", "c++", "python", "php"));
    }
}




/*
run:

[java, c, c++, python, php]

*/

 



answered Feb 17, 2021 by avibootz
edited Feb 17, 2021 by avibootz

Related questions

1 answer 172 views
4 answers 119 views
3 answers 229 views
...