Java:如何声明一个数组并快速用数据填充它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4051036/
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 declare an array and quickly fill its with data?
提问by Saska
public static void main(String[] args) throws IOException {
String st3[]=new String[]{"сто", "двести", "триста", "sdf", "sdfsd", "sdfsd"};
System.out.println(st3[1]);
}
In second line Netbeans displays an error:
在第二行 Netbeans 显示错误:
"non-static variable cannot be referenced from a static context".
I know that the problem is in declaring the array. How to declare a STRING array and quickly fill it with data?
我知道问题在于声明数组。如何声明一个字符串数组并快速填充数据?
Sorry for stupid question and very bad english.
对不起,愚蠢的问题和非常糟糕的英语。
Thanks very much for answers, error resolved. :)
非常感谢您的回答,错误已解决。:)
回答by Jon Skeet
The problem isn't to do with declaring an array at all. You haven't shown enough code to show what really iswrong with it, but it's not those lines themselves. The array initialization is a little bit long-winded, but it's valid.
问题根本与声明数组无关。您还没有表现出足够的代码,以显示真正的不对的地方,但它不是那些线本身。数组初始化有点冗长,但它是有效的。
Please show a short but complete program which demonstrates the problem.
请展示一个简短但完整的程序来演示问题。
Which method are these lines in?
这些行在哪种方法中?
Here's a short but complete program which doeswork:
这是一个简短但完整的程序,它确实有效:
public class Test {
public static void main(String[] args) {
String st3[]=new String[]{"x", "y", "z", "sdf", "sdfsd", "sdfsd"};
System.out.println(st3[1]);
}
}
回答by bancer
The shortest way to declare and fill in an array:
声明和填充数组的最短方法:
String[] st3 = {"сто", "двести", "триста", "sdf", "sdfsd", "sdfsd"};