Methods of deformable parameters in java

  • Variable number of parameters (String... args)
  • 1. What's new in jdk5.0
  • 2. Specific use:
  • 2.1 The format of the variable number of formal parameters: data type...variable name
  • 2.2 When calling a method with a variable number of formal parameters, the number of actual parameters passed in can be zero, one or more than one
  • 2.3 The method of a variable number of formal parameters has the same name as the method in this class, and methods with different formal parameters constitute an overload
  • 2.4 The method of the variable number of formal parameters has the same name as the method in this class, and the arrays with the same formal parameter type do not constitute overloading.
  • 2.5 The variable number of formal parameters must be declared at the end of the method
  • 2.6 Variable number of formal parameters In the formal parameters of a method, at most one deformable parameter can be declared
 public static void main(String[] args) {
    
    
      MethodArgsTest test=new MethodArgsTest();
      test.show("可能我浪荡,让人家不安","说谎");
      test.show(1,"可能我浪荡,让人家不安","说谎");
    }
    public void show(String... str){
    
    
        System.out.println(Arrays.toString(str));
        for (int i = 0; i < str.length; i++) {
    
    
            System.out.println(str[i]);
        }
    }
    public void show(int a,String... str){
    
    
        System.out.println(Arrays.toString(str));
        for (int i = 0; i < str.length; i++) {
    
    
            System.out.println(str[i]);
            System.out.println(a);
        }
    }

Guess you like

Origin blog.csdn.net/dwjdj/article/details/113039690