Object... args是可变参数,一般用于传递时不知道参数的个数,或者参数个数不固定,Object[] args是数组,需要实现定义好一个数组,然把数据放入到数组中,再将数组传递到方法中。
- public class Test {
- public static void main(String[] args) {
- method1("a","b","c");
- method1("a","b","c","d");
-
- Object[] obj = new Object[]{"x","y","z"};
- method2(obj);
- }
-
- public static void method1(Object...obj){
- System.out.println(Arrays.toString(obj));
- }
-
- public static void method2(Object[] obj){
- for(int i=0;i<obj.length;i++){
- System.out.print(obj[i]+" ");
- }
- }
- }
复制代码 运行结果为:[a, b, c]
[a, b, c, d]
x y z
|