2010-04-01 142 views
136

错误Java:如何初始化String []?

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

代码

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

回答

229

您需要initializeerrorSoon,该错误消息指示,你只有declared它。

String[] errorSoon;     // <--declared statement 
String[] errorSoon = new String[100]; // <--initialized statement 

需要初始化数组,因此它可以为String元素,然后才能开始设置指标分配正确的存储器。

如果你只声明数组(像你一样)没有分配给String元素内存,但只是一个参考手柄errorSoon,当你试图在任何索引初始化变量会抛出一个错误。

作为一个侧面说明,你也可以初始化String阵列内部括号,{ }因为如此,

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

这相当于

String[] errorSoon = new String[2]; 
errorSoon[0] = "Hello"; 
errorSoon[1] = "World"; 
+3

这是你不能使用贝蒂()来实例化每一个字符串你的数组有一个默认值。一个包含5个空字符串的数组应该是= new Array [5](“”);而不是= {“”,“”,“”,“”,“”}。 – 2015-04-15 11:19:57

+0

使用for循环。 – 2016-09-27 08:59:20

6
String[] errorSoon = new String[n]; 

,N是多少字符串它需要保持。

您可以在声明中做到这一点,或者稍后在没有String []的情况下做到这一点,只要在尝试使用它们之前。

23
String[] errorSoon = { "foo", "bar" }; 

- 或 -

String[] errorSoon = new String[2]; 
errorSoon[0] = "foo"; 
errorSoon[1] = "bar"; 
104
String[] args = new String[]{"firstarg", "secondarg", "thirdarg"}; 
+2

也许不是OP的问题标题建议的确切内容,但我试图将我的字符串传递给接受String []的参数,这是解决方案 – kommradHomer 2014-03-31 13:09:39

+0

难道你不能忽略新的String btw吗? String [] output = {“”,“”,“”};似乎在我的代码中工作。 – 2015-04-15 11:20:49

+2

如果你已经初始化你的数组,并且你想重新初始化它,那么你不能'args = {“new”,“array”};' 你必须'args = new String [] {“新“,”数组“};' – Darpan 2015-05-08 06:16:39

1

你总是可以写这样的

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. 
} 
9

我相信你只是在C++迁移,在Java中,你必须初始化一个数据类型(除了原始类型和字符串不被视为java中的原始类型)以根据其规范使用它们如果你不那么它就像一个空的引用变量(很像C++上下文中的指针)。

public class StringTest { 
    public static void main(String[] args) { 
     String[] errorSoon = new String[100]; 
     errorSoon[0] = "Error, why?"; 
     //another approach would be direct initialization 
     String[] errorsoon = {"Error , why?"}; 
    } 
} 
0

字符串宣言:

String str; 

字符串初始化

String[] str=new String[3];//if we give string[2] will get Exception insted 
str[0]="Tej"; 
str[1]="Good"; 
str[2]="Girl"; 

String str="SSN"; 

我们可以在字符串获取个性:

char chr=str.charAt(0);`//output will be S` 

如果我想获得个性ASCII值是这样的:

System.out.println((int)chr); //output:83 

现在我想ASCII值转换成性格特征/符号。

int n=(int)chr; 
System.out.println((char)n);//output:S 
0
String[] string=new String[60]; 
System.out.println(string.length); 

它是初始化和获取字符串长度的代码非常简单的方法,适合初学者

2

的Java 8,我们还可以使用流如

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

在情况下,我们已经有一个字符串(stringList)的名单,然后我们就可以收集到字符串数组为:

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