干预数组和无数的Google搜索之后,我似乎找不到答案。
public static void main(String args[]){
String[] names = new String[4]; //I want to fill this up with data from country
country(names);
System.out.println(names(0)) //I want this to display Madrid
}
public static void country(String[] names){
names[0] = "Madrid";
names[1] = "Berlin";
return;
}
我不确定这是否可以解释我要做什么。
最佳答案
您确实必须使用Java语法。您的代码非常简单,因此可以立即使用,但是您必须小心一些细节,下面的代码可以正常工作:
public static void main(String args[]) {
String[] names = new String[4]; //I want to fill this up with data from country
country(names);
System.out.println(names[0]); //I want this to display Madrid
}
public static void country(String[] names) {
names[0] = "Madrid";
names[1] = "Berlin";
}
如您所见,我使用[]访问数组中特定索引处的值。我也不在void方法中使用任何返回值。
您不需要在country方法中返回数组,因为Java不会在value上传递参数(请参见http://javarevisited.blogspot.fr/2012/12/does-java-pass-by-value-or-pass-by-reference.html)
因此,我真的建议您阅读可以找到的有关Java语法的任何教程,以提高自己的水平。
关于java - 如何用另一种方法的数据填充一个字符串数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26187855/