解决接收不固定参数个数的参数传值问题
当一个方法被多个地方调用,然而调用的时候为该方法传入的参数的个数是不固定的的时候怎么办呐?
那么我们可以把方法设置为不固定接受参数个数的形式。如下测试类:
public static void test(String t1,String t2,Map ...maps) {
//这里传入的参数的个数为大于等于2个,当maps为空,传入参数是两个,否则就是多个。
if(maps.length>0){
System.out.println(maps.length);
System.out.println("执行带参数推送");
for (int i = 0; i < maps.length; i++) {
System.out.println(maps[i]);
}
}else{
System.out.println(maps.length);
System.out.println("我是没有执行带参数推送");
}
}
public static void main(String[] args) throws Exception {
Map<String, Object> resultMap1 = new HashMap<String, Object>();
Map<String, Object> resultMap2 = new HashMap<String, Object>();
resultMap1.put("map1", "中国国旅公司");
resultMap1.put("map2", "28163");
resultMap2.put("map1", "中国国旅公司");
resultMap2.put("map2", "28163");
test("参数1","参数2",resultMap1,resultMap2);
//或
test("参数1","参数2");//也可以
}
2、一个小技巧(如何打印出目录符号,兼容不同的操作系统)
打印目录符号
public static void main(String[] args) {
System.out.println(File.separator);
}