调用方法 - 为什么这不起作用? - Java

时间:2014-12-08 04:52:06

标签: java methods

 public static void main(String[] args) {

            getRandomPrints({"a" , "b" ,"c" , "d"});

        }

        public static void getRandomPrints(String[] args){
             System.out.printLn("Hello World" + arg")
        }

我在调用此方法时遇到问题,我想知道我在这里做错了什么。 它说

The method getRandomPrints(String[]) in the type test is not applicable for the arguments()

我想知道这里有什么不对,谢谢

2 个答案:

答案 0 :(得分:3)

试试这种方式

public static void main(String[] args) {

        getRandomPrints(new String[]{"a" , "b" ,"c" , "d"});

    }

    public static void getRandomPrints(String[] args){
         System.out.println("Hello World" + args);
    }

我修复的编译时错误也很少,如println

答案 1 :(得分:1)

您没有将正确的参数传递给方法:

getRandomPrints({"a" , "b" ,"c" , "d"});// not a valid array

这里也有一些更正:

System.out.printLn("Hello World" + args);
            `l` ^            note `s` ^ ^ semicolon was missing

您可以试试这个:

public static void main(String[] args) {
    getRandomPrints(new String[]{"a" , "b" ,"c" , "d"});//creating new array of string
}

public static void getRandomPrints(String[] args){
    for(String s : args)
       System.out.println(s);
}

varargs

public static void main(String[] args) {
    getRandomPrints("a" , "b" ,"c" , "d");
}

public static void getRandomPrints(String... args){//varargs to handle csv as array
    for(String s : args)
       System.out.println(s);
}

split()

public static void main(String[] args) {
    getRandomPrints("a,b,c,d".split(","));
}

public static void getRandomPrints(String[] args){
    for(String s : args)
       System.out.println(s);
}