简体   繁体   中英

How to declare multiple parameters with the same data type? (java)

When declaring multiple variables, I can just use this syntax

int a, b, c; 

But since the comma , is used to separate deceleration of parameters of the method, I can't use the above syntax

public void method (int a, int b, int c) { }

So, how can I declare multiple parameters without typing the same data type multiple times?

You could try a constructor? Something like:

    public ExampleConstructor(int a, int b, int c)
    {
      this.a = a;
      this.b = b;
      this.c = c;
    }

Then in your method just pass the constructor variables. You can use getters in your method to fetch the desired variable:

    public void method (ExampleConstructor eC)
    {
        whateverMethodIsDoing = eC.getA;
    }

This may be my first response to a question at stackOverflow, I hope it is useful.

try below method.

  public void method(int... intVar){

      for(int num: intVar){

         System.out.println(num);

       }

   }

and

method(1,2,3,4,5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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