Java variable parameters -08 days study notes

variable parameter

  • In the method declaration, add an ellipsis (...) after the specified parameter type
  • Only one variable parameter can be specified in a method, and it must be the last parameter of the method. Any ordinary parameters must be declared before it
public void test (intx,int....i)  //放在形式参数最后面
package com.xin.method;

public class Demo05 {
    
    
    public static void main(String[] args) {
    
    
        Demo05 demo05 = new Demo05();
       demo05.method(1,2,23,2,1); //这里的i的数字对应下面的i[];


    }

    public static void method(int a, int... i){
    
    
        System.out.println(i[0]); //i[0]代表第0个值
        System.out.println(i[1]); //i[1]代表第1个值
        System.out.println(i[2]); //i[2]代表第2个值
        System.out.println(i[3]); //i[3]代表第3个值
    }
}



Guess you like

Origin blog.csdn.net/yibai_/article/details/114497751
Recommended