定义java方法可变参数

以校验参数非空为例:


public class checkParams {
    //非空校验
	public static boolean checkString(String ... params) {
		boolean boo=true;
		if(params ==null||"".equals(params)) {
			return false;
		}
		
		for (int i = 0; i < params.length; i++) {
			if(params[i]==null||"".equals(params[i])) {
				boo=false;
			}
		}
		return boo;
	}
	public static void main(String[] args) {
        //false
		boolean boo0 = checkParams.checkString("");
        //false
		boolean boo1 = checkParams.checkString("","1");
        //false
		boolean boo2 = checkParams.checkString(null,"1");
        //true
		boolean boo3 = checkParams.checkString("2","1");
	}
}

 

你可能感兴趣的:(JAVA基础)