日期:2014-05-20 浏览次数:20940 次
import java.lang.reflect.*;
public class Test78 {
public static void main(String[] args) {
int[] a = new int[] { 1, 2, 3 };
a = new int[] { 4, 5, 6 }; //可以在任意地方创建和初始化数组对象
print(new int[] { 7, 8, 9 }); //可以在任意地方创建和初始化数组对象
int[] b = { 1, 2, 3 }; // 前一种形式的简写形式
// b = { 4, 5, 6 }; 必须在定义时候使用
}
//打印数组的方法
public static void print(Object o) {
Class cl = o.getClass();
if (!cl.isArray())
return;
Class componentType = cl.getComponentType();
int length = Array.getLength(o);
System.out.print(componentType.getName() + "[" + length + "] = { ");
for (int i = 0; i < length; i++) {
System.out.print(Array.get(o, i));
if (i != length - 1)
System.out.print(",");
System.out.print(" ");
}
System.out.println("}");
}
}