简体   繁体   English

如何在函数内部定位 Java 中 Varargs 方法的元素?

[英]How to target the elements of the Varargs method in Java inside the function?

I wanted to use the varags method to take the input of various integers and find out the maximum & minimum among them.我想使用 varags 方法来获取各种整数的输入,并找出其中的最大值和最小值。

public class MINMAX {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        Maximum(a,b,c);
        Minimum(a,b,c);
    }
    static void Maximum(int ...v) {
        int max = Math.max(a,Math.max(b,c));
        System.out.println("The Largest no. is: "+max);
    }
    static void Minimum(int ...v) {
        int min = Math.min(a,Math.min(b,c));
        System.out.println("The Smallest no. is: "+min);
    }
}

Here, I'm taking input of a,b,c from the user to find out the max & min.在这里,我从用户那里输入 a、b、c 来找出最大值和最小值。 but want to know how can i target the specific elements in Varargs Method.但想知道如何针对 Varargs 方法中的特定元素。

To begin, imagine how many Math.min you need if you have 10 inputs instead of 3.首先,想象一下如果您有 10 个输入而不是 3 个输入,您需要多少Math.min

What you need is a method which can accept different number of inputs(that's why we have varargs), so that there is no need to change anything when you want to support more inputs.您需要的是一种可以接受不同数量输入的方法(这就是我们有可变参数的原因),因此当您想要支持更多输入时无需更改任何内容。 Hence you need to handle varargs(which can be treated as an array) instead of specific element.因此,您需要处理可变参数(可以被视为数组)而不是特定元素。

In addition, by using IntStream#summaryStatistics , you can get min and max in one shot.此外,通过使用IntStream#summaryStatistics ,您可以一次获得最小值和最大值。

    static void maximumAndMaximum(int... v) {
        IntSummaryStatistics statistics = IntStream.of(v).summaryStatistics();
        System.out.println("The Smallest no. is: " + statistics.getMin());
        System.out.println("The Largest no. is: " + statistics.getMax());
    }

The varargs syntax ( ... parameterName ) is just another way of saying that the parameter is an array isn't it?可变参数语法 ( ... parameterName ) 只是另一种说法,参数是一个数组,不是吗? The java.util.Collections class has methods on it that you might find useful. java.util.Collections类具有您可能会发现有用的方法。 I suspect you may need to convert the array into something that implements the Collection interface to use them (eg java.util.ArrayList ).我怀疑您可能需要将数组转换为实现Collection接口的东西才能使用它们(例如java.util.ArrayList )。

You can access it as an int primitive type array that you have given in the method parameter.您可以将其作为方法参数中给出的 int 原始类型数组进行访问。 eg例如

 static void Maximum(int ...v) {
      int max = Math.max(v[0],Math.max(v[1],v[2]));
      System.out.println("The Largest no. is: "+max);
 }

 static void Minimum(int ...v) {
     int min = Math.min(v[0],Math.min(v[1],v[2]));
     System.out.println("The Smallest no. is: "+min);
 }

I recommend that you make it a habit to check the size of the directory and other controls before starting the process.我建议您养成在开始该过程之前检查目录大小和其他控件的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM