How to define a method that can take arbitrary arguments in java?

How to define a method that can take arbitrary arguments in java? Is there a demo?

+3
source share
6 answers

varargs were introduced in Java 5.

For instance:

public String join(String... parts);

This is actually a shortcut to:

public String join(String[] parts);

The parameter is partsused as an array in the method, but the method can be called without building an array (for example, obj.join(new String[] {part1, part2, part3}))

However, be very careful with this because ambiguities may arise. For instance:

public void write(String author, String... words);
public void write(String... words);

What method will be called if obj.write("Mike", "jumps")? The compiler is smart enough to detect ambiguity, but I had cases where some compilers did not find such problems (I don’t remember exactly)

varargs , , , . . :

public String publishBook(String title, [String author], 
      [String isbn], [boolean hardcover]); // [..] would mean optional

+10

+3

. :

    @SafeVarargs
    public void callMe(Object... args) { ... }

callMe(1, "hallo", 3.14159, new Object()), . , , @SafeVarargs, ( ).

, - :

    @SuppressWarnings("unchecked")
    public <T> void callMe(T... args);

, , , callMe , . `callMe ( "a", "b", "c" ). , , , :

    ArbitraryCallable x = new ArbitraryCallableImplementation();
    x.callMe("a", "b"); // no warning
    x.callMe(1, "x", 3.14159); // warning

, Object... , Object[], `callMe ( Object [] { "a" }), : callMe ( ) ?

+1

. :

public void foo (ParamType ... name) { 

}
0

varargs ( , , - , Java , , ).

Java , , Object. , , .

varargs:

public static void printAll(String... args){
   for (String str : args ){
       System.out.println(str);
   }
}
// Now can write printAll("Hello", "world", "etc", "etc");

:

public static <T> void print(T obj){
    System.out.println(obj.toString());
}
0

, java ( ), varargs ( ). OO , Builder, , . ( Effective Java 2nd Edition)

 public class NutritionFacts {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;

    public static class Builder {
        // Required parameters
        private final int servingSize;
        private final int servings;
        // Optional parameters - initialized to default values
        private int calories = 0;
        private int fat = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder calories(int val) {
            calories = val;
            return this;
        }

        public Builder fat(int val) {
            fat = val;
            return this;
        }

        public NutritionFacts build() {
            return new NutritionFacts(this);
        }

    }

    private NutritionFacts(Builder builder) {
        servingSize = builder.servingSize;
        servings = builder.servings;
        calories = builder.calories;
        fat = builder.fat;
    }

}

-

NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8). 
calories(100).fat(35).build(); 

Now it’s really cool that you can add methods that actually accept varargs too, which makes this particular template very powerful. This template also makes your object immutable and saves you from writing telescope designers. What he will not do is your dishes, but I live in hope;)

0
source

Source: https://habr.com/ru/post/1737890/


All Articles