可变参数列表 java
2017-09-02 22:39
330 查看
Thinking in Java(3)
1. 可变参数列表
1.1 使用Object数组参数
import java.util.*; class A {} public class VarArgs { static void printArray(Object[] args) { for(Object obj : args) { System.out.println(obj + " "); } System.out.println(); } public static void main(String[] args) { printArray(new Object[] {new Integer(47), new Float(3.14), new Double(11.11)}); printArray(new Object[] {"one", "two", "three"}); printArray(new Object[] { new A(), new A(), new A() }); } } result 47 3.14 11.11 one two three A@69d4c4b7 A@fbf00a9 A@44c45f52
最后一个结果,如果java类没有实现toString()方法的话,那么默认输出类名和地址。
1.2 使用可变参数列表
import java.util.*; class A {} public class NewVarArgs { static void printArray(Object... args) { for(Object obj : args) { System.out.println(obj + " "); } System.out.println(); } public static void main(String[] args) { printArray(new Integer(47), new Float(3.14), new Double(11.11)); printArray(47, 3.14, 11.11); printArray("one", "two", "three"); printArray(new A(), new A(), new A()); printArray((Object[])new Integer[] {1, 2, 3, 4}); printArray(); } } 47 3.14 11.11 47 3.14 11.11 one two three A@384e23c3 A@120df416 A@5213d99c 1 2 3 4
从上面可以看出,可变参数列表可以为空。
相关文章推荐
- java基础(九) 可变参数列表介绍
- Java可变参数列表
- Java可变参数列表详解
- Java 可变参数列表
- JAVA类型后面跟3个点(可变长度参数列表)
- JAVA类型后面跟3个点(可变长度参数列表)
- Java可变参数列表说明(int... array)
- Thinking in java 之:可变参数列表
- Java可变长参数列表
- 从头认识java-4.8 数组的初始化(2)-可变参数列表
- Java 可变参数列表
- Java的可变参数列表
- Java 可变长参数列表
- Java学习笔记003——继承、抽象类、类相等测试、泛型数组列表、可变参数方法
- java中可变参数列表重载问题
- java 函数可变参数列表
- Java的可变参数列表
- java可变长参数列表
- 从头认识java-4.8 数组的初始化(2)-可变参数列表
- java可变参数列表