Java array syntax substitution

See English answers > What does int []... Arrays mean in Java? 8

yes

public void fn(Character[]...){}

amount to

public void fn(Character[]){} //(i.e. "..." is redundant)

Or equivalent

public void fn(Character[][]){}

Solution

With varargs, you will allow multiple parameters, only one 2D array, and only one parameter They may produce a 2D array, but they are called in very different ways

Small sample:

public class Test {
    public static void main(String[] args){
        doSomething(new char[5],new char[6]);
        doSomething(new char[2][3]);
    }

    private static void doSomething(char[]... args){
        for(char[] s : args){
            System.out.println(s.length);
        }
    }
}

Well done:

5
6
3
3

However, if you only use char [], the first call method will be invalid That's the difference

Char vs char has no functional difference in this case, although you may want to remember the last sentence of this answer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>