Java in the magical indefinite term parameters (variable parameters)

Java in the magical indefinite term parameters (variable parameters)

problem

Many times we write Java will encounter a problem, that is the same way, we need to pass a different number of parameters, the first call may be passed four parameters, the second call must pass five parameters

appendString1("Int数据:", 2, 3, 5, 7);

appendString1("Int数据:", 15, 16, 11, 12, 13);

Look like this, everyone will say: I know, this is heavy-duty thing. The same function name, parameter types and different number of reload function can be done, only calls a function.

However, heavy-duty, then also you need to write twice function . 2 Fortunately, many parameters when it will be tedious, but each time the expansion will be very troublesome.

Some people say: That with the array, the array this is no problem.

appendString1(String s, int[] numbers);

This is a method to solve the problem, but also to construct such an array of objects.

Indefinite term parameters (variable parameters)

And the number of arguments may be not the same parameter in Python, only need to specify the parameters in the time of the call. However, Java can not do, so Java was introduced in the indefinite term parameters (variable parameter) concept.
Indefinite term parameter syntax is as follows:

返回值 方法名(参数类型...参数名称)
//是的,你没有看错就是省略号,格式就是这样,不是我省略了什么。
public static String appendString1(String s, int... numbers);

Doing so can solve the problem of the number of uncertain parameters.

int ... Numbers can be understood as an int array, so int ... in the method parameter and int [] can not be overloaded because of the same nature.

//编译无法通过,因为本质上是一样的无法完成重载
public void method(int...args);
public void method(int[] args);

Precautions

  1. Variable parameter entry must be placed in a final argument list.
    the reason:

    //编译错误,因为前面不定项,系统判断不出来最后一个int是int数组元素还是i
    public void method(int...args, int i)    
    
  2. Indefinite term parameter can only have (more, there must be a not the last) a.

    //编译错误,提示不定项的参数必须放最后,系统不知哪些属于args1,哪些属于args2
    public void method(int...args1, int...args2)   
    public void method(int i, int j, int...args)   //正确用法
    public void method(String s, double d, int...args)   //正确用法
    
  3. When overloaded, run priority issue.

    After the overload on the priority, a method of determining the priority of parameters than the method of the variable parameter entry

This is a good use, you do not need to write as many times overloaded, if the type of problems, you can fit Object / + generic term variable parameters.

 public static <T> String appendString1(String s, T... ts) {
        for (int i = 0; i < ts.length; i++) {
            s += (ts[i] + ",");
        }
        return s;
    }

If you feel good, then please little praise, Oh also welcome FAVORITE

Here Insert Picture Description

Published 41 original articles · won praise 94 · views 9567

Guess you like

Origin blog.csdn.net/qq_41718454/article/details/104309010