文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Java 在学习二十九【原创】  


数组的类类型的表示:

java.lang.reflect.Array 类学习


package coreJava;


import java.lang.reflect.Array;

import java.lang.reflect.Method;


public class Test3 {


public static void main(String[] args) {

// TODO Auto-generated method stub

int[] a = {1 ,2 ,3};

Integer[] b = {1, 2, 3, 4, 5};

Object o1 = a; //ok

//Object[] o2 = a; //no

Object o3 = b; //ok

Object[] o5 = b; //ok

String[][] ss = {{"aa", "bb"}, {"cc", "dd"}};

Object o6 = ss; //ok

Object[] o7 = ss; //ok 把每个一维数组看做是一个对象

Object[][] o8 = ss;//ok 把每一个字符串看做是一个对象

try {

Class<C> c = C.class;

Method m = c.getMethod("test", new Class[]{String[][].class, 

int[].class,double[].class, int.class});

m.invoke(c.newInstance(), new Object[]{new String[][]{}, new int[]{}, new double[]{}, 10});

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("#######################");

MainTest.main(new String[] {"hello", "anan"});

try {

Method m = MainTest.class.getMethod("main", new Class[] {String[].class});

m.invoke(null, new Object[] {new String[] {"hello", "anan"}});

} catch(Exception e) {

e.printStackTrace();

}

System.out.println("#######################");

MainTest.printObejct(new int[] {1 ,2 ,3});

MainTest.printObejct(new String[][] {{"hello", "anan"},{"hello", "java"}});

}


}


class C {

public void test(String[][] str, int[] a, double[] c, int d) {

System.out.println(str);

System.out.println(a);

System.out.println(c);

System.out.println(d);

}

}


class MainTest {

public static void main(String[] args) {

for (String s: args) {

System.out.println(s);

}

}

public static void printObejct(Object object) {

Class<? extends Object> c = object.getClass();

if (c.isArray()) {

int length = Array.getLength(object);

for (int i = 0 ; i < length; i++) {

Object obj = Array.get(object, i);

printObejct(obj);

}

} else {

System.out.println(object);

}

}

}