Java: how to initialize String[]?

后端 未结 11 2180
渐次进展
渐次进展 2020-12-22 18:57

Error

% javac  StringTest.java 
StringTest.java:4: variable errorSoon might not have been initialized
        errorSoon[0] = \"Error, why?\"         


        
11条回答
  •  独厮守ぢ
    2020-12-22 19:23

    In Java 8 we can also make use of streams e.g.

    String[] strings = Stream.of("First", "Second", "Third").toArray(String[]::new);
    

    In case we already have a list of strings (stringList) then we can collect into string array as:

    String[] strings = stringList.stream().toArray(String[]::new);
    

提交回复
热议问题