错误

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

Code

public class StringTest {
        public static void main(String[] args) {
                String[] errorSoon;
                errorSoon[0] = "Error, why?";
        }
}

当前回答

在Java 8中,我们还可以使用流。

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

如果我们已经有一个字符串列表(stringList),那么我们可以收集到字符串数组为:

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

其他回答

String[] arr = {"foo", "bar"};

如果你将一个字符串数组传递给一个方法,执行以下操作:

myFunc(arr);

或做的事:

myFunc(new String[] {"foo", "bar"});

你可以这样写

String[] errorSoon = {"Hello","World"};

For (int x=0;x<errorSoon.length;x++) // in this way u create a for     loop that would like display the elements which are inside the array     errorSoon.oh errorSoon.length is the same as errorSoon<2 

{
   System.out.println(" "+errorSoon[x]); // this will output those two     words, at the top hello and world at the bottom of hello.  
}

您可以使用下面的代码初始化大小并将空值设置为字符串数组

String[] row = new String[size];
Arrays.fill(row, "");
String[] args = new String[]{"firstarg", "secondarg", "thirdarg"};

在Java 8中,我们还可以使用流。

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

如果我们已经有一个字符串列表(stringList),那么我们可以收集到字符串数组为:

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