计算传递给方法的参数数量

huangapple go评论70阅读模式
英文:

Count passed arguments to a method

问题

以下是翻译好的部分:

在Java中是否有可能计算传递给方法的参数数量

有类似这样的内容

public class practise7 {

public static void main(String[] args) {

        int[] array3 = new int[]{1};
        int[] array4 = new int[]{1, 3, 4};
        int[] array5 = new int[]{2, 3,};

        combine(array3, array4, array5);
        
    }
   
    public static void combine(int[] array3, int[] array4, int[] array5) {
        //在这里我需要传递的参数数量(这里是3,例如)

        int count = args.length; //在谷歌上找到了这个,但没有起作用
        System.out.println(count);
        
    }
}

非常感谢!

英文:

is there a possibility in java to count the number of passed arguments into a method?

Got something like this:

public class practise7 {

public static void main(String[] args) {

        int[] array3 = new int[]{1};
        int[] array4 = new int[]{1, 3, 4};
        int[] array5 = new int[]{2, 3,};

        combine(array3, array4, array5);
        
    }
   
    public static void combine(int[] array3, int[] array4, int[] array5) {
        //Here i need the number of passed arguments (here 3 e.g.)

        int count = args.length; //found this on google but didn't worked
        System.out.println(count);
        
    }
}

Thanks a lot!

答案1

得分: 3

请试试这个。它使用了变长参数(varargs)语法。

public static void main(String[] args) {

    int[] array3 = new int[]{1};
    int[] array4 = new int[]{1, 3, 4};
    int[] array5 = new int[]{2, 3,};

    combine(array3, array4, array5);
    
}

// 使用变长参数语法
public static void combine(int[]... v) {
    // 这里我需要传递的参数数量(这里是 3 个)

    int count = v.length; 
    System.out.println(count);

    for (int[] k : v) {
      System.out.println(Arrays.toString(k));
    }
}
}

输出结果:

3
[1]
[1, 3, 4]
[2, 3]

请注意,将数组和非数组组合在参数列表中有时可能会产生意外的结果。变长参数语法的参数必须位于方法签名中的最后一个位置。

英文:

Try this. It uses the var...arg syntax.

public static void main(String[] args) {

        int[] array3 = new int[]{1};
        int[] array4 = new int[]{1, 3, 4};
        int[] array5 = new int[]{2, 3,};

        combine(array3, array4, array5);
        
    }
   // uses the variable arguments syntax
    public static void combine(int[]...v) {
        //Here i need the number of passed arguments (here 3 e.g.)

        int count = v.length; 
        System.out.println(count);

        for (int[] k : v) {
          System.out.println(Arrays.toString(k));
        }
    }
}

Prints

3
[1]
[1, 3, 4]
[2, 3]

Note that combining arrays and non-arrays in the argument list can sometimes provide unexpected results. The variable syntax argument must be the last one in the signature.

答案2

得分: 2

你的解决方案不起作用,因为使用 'args.length' 只能获得在主函数中传递的参数数量。你可以使用Java的可变参数特性,如下所示:

public static void combine(int[] ... arrays) 
{
        int count = arrays.length;
        System.out.println(count);
}
英文:

Your solution doesn't work because with 'args.length' you can only get the number of arguments that were passed in the main function. You can use variable arugments feature of Java as follows:

public static void combine(int[] ... arrays) 
{
        int count = arrays.length;
        System.out.println(count);
}

huangapple
  • 本文由 发表于 2020年10月8日 01:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64249343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定