相反,尝试类似... takesStringArray(new String [] {"foo"}); How do I pass a array without making it a seperate variable? For example I know this works:class Test{ public static void main(String[] args){ String[] arbitraryStrings={"foo"}; takesStringArray(arbitraryStrings); } public static void takesStringArray(String[] argument){ System.out.println(argument); }}But I dont want to make the array a variable as it is only used here. Is there any way to do something like this:class Test{ public static void main(String[] args){ takesStringArray({"foo"}); } public static void takesStringArray(String[] argument){ System.out.println(argument); }} 解决方案 {"foo"} doens't tell Java anything about what type of array you are trying to create...Instead, try something like...takesStringArray(new String[] {"foo"}); 这篇关于如何将一个函数传递给数组文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 10:29