Java:如何初始化 String[]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2564298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: how to initialize String[]?
提问by hhh
Error
错误
% 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?";
}
}
采纳答案by Anthony Forloney
You need to initializeerrorSoon
, as indicated by the error message, you have only declaredit.
您需要初始化errorSoon
,如错误消息所示,您只是声明了它。
String[] errorSoon; // <--declared statement
String[] errorSoon = new String[100]; // <--initialized statement
You need to initialize the array so it can allocate the correct memory storage for the String
elements beforeyou can start setting the index.
在开始设置索引之前,您需要初始化数组,以便它可以为String
元素分配正确的内存存储。
If you onlydeclare the array (as you did) there is no memory allocated for the String
elements, but only a reference handle to errorSoon
, and will throw an error when you try to initialize a variable at any index.
如果您只声明数组(如您所做的那样),则不会为String
元素分配内存,而只会为分配一个引用句柄errorSoon
,并且当您尝试在任何索引处初始化变量时都会抛出错误。
As a side note, you could also initialize the String
array inside braces, { }
as so,
作为旁注,您还可以String
在大括号内初始化数组,{ }
因此,
String[] errorSoon = {"Hello", "World"};
which is equivalent to
这相当于
String[] errorSoon = new String[2];
errorSoon[0] = "Hello";
errorSoon[1] = "World";
回答by AaronM
String[] errorSoon = new String[n];
With n being how many strings it needs to hold.
n 是它需要保存多少个字符串。
You can do that in the declaration, or do it without the String[] later on, so long as it's before you try use them.
您可以在声明中执行此操作,也可以稍后在没有 String[] 的情况下执行此操作,只要在您尝试使用它们之前即可。
回答by Taylor Leese
String[] errorSoon = { "foo", "bar" };
-- or --
- 或者 -
String[] errorSoon = new String[2];
errorSoon[0] = "foo";
errorSoon[1] = "bar";
回答by Yauhen
String[] args = new String[]{"firstarg", "secondarg", "thirdarg"};
回答by Gopolang
You can always write it like this
你总是可以这样写
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.
}
回答by Syed Tayyab Abbas
I believe you just migrated from C++, Well in java you have to initialize a data type(other then primitive types and String is not a considered as a primitive type in java ) to use them as according to their specifications if you don't then its just like an empty reference variable (much like a pointer in the context of C++).
我相信你刚刚从 C++ 迁移过来,那么在 java 中你必须初始化一个数据类型(其他原始类型和 String 在 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?"};
}
}
回答by Shiva
String Declaration:
字符串声明:
String str;
String Initialization
字符串初始化
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";
We can get individual character in String:
我们可以在字符串中获取单个字符:
char chr=str.charAt(0);`//output will be S`
If I want to to get individual character Ascii value like this:
如果我想获得这样的单个字符 Ascii 值:
System.out.println((int)chr); //output:83
Now i want to convert Ascii value into Charecter/Symbol.
现在我想将 Ascii 值转换为字符/符号。
int n=(int)chr;
System.out.println((char)n);//output:S
回答by Asfer Hussain Siddiqui
String[] string=new String[60];
System.out.println(string.length);
it is initialization and getting the STRING LENGTH code in very simple way for beginners
它是初始化并以非常简单的方式为初学者获取 STRING LENGTH 代码
回答by akhil_mittal
In Java 8we can also make use of streams e.g.
在Java 8 中,我们也可以使用流,例如
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:
如果我们已经有一个字符串列表 ( stringList
) 那么我们可以收集到字符串数组中:
String[] strings = stringList.stream().toArray(String[]::new);
回答by Ali Sadeghi
You can use below code to initialize size and set empty value to array of Strings
您可以使用下面的代码来初始化大小并将空值设置为字符串数组
String[] row = new String[size];
Arrays.fill(row, "");