實例講解Java編程中數(shù)組反射的使用方法
什么是反射
“反射(Reflection)能夠讓運行于JVM中的程序檢測和修改運行時的行為。”這個概念常常會和內(nèi)?。↖ntrospection)混淆,以下是這兩個術(shù)語在Wikipedia中的解釋:
- 內(nèi)省用于在運行時檢測某個對象的類型和其包含的屬性;
- 反射用于在運行時檢測和修改某個對象的結(jié)構(gòu)及其行為。
- 從它們的定義可以看出,內(nèi)省是反射的一個子集。有些語言支持內(nèi)省,但并不支持反射,如C++。
內(nèi)省示例:instanceof 運算符用于檢測某個對象是否屬于特定的類。
if (obj instanceof Dog) {
Dog d = (Dog) obj;
d.bark();
}
反射示例:Class.forName()方法可以通過類或接口的名稱(一個字符串或完全限定名)來獲取對應的Class對象。forName方法會觸發(fā)類的初始化。
// 使用反射
Class<?> c = Class.forName("classpath.and.classname");
Object dog = c.newInstance();
Method m = c.getDeclaredMethod("bark", new Class<?>[0]);
m.invoke(dog);
在Java中,反射更接近于內(nèi)省,因為你無法改變一個對象的結(jié)構(gòu)。雖然一些API可以用來修改方法和屬性的可見性,但并不能修改結(jié)構(gòu)。
數(shù)組的反射
數(shù)組的反射有什么用呢?何時需要使用數(shù)組的反射呢?先來看下下面的代碼:
Integer[] nums = {1, 2, 3, 4};
Object[] objs = nums; //這里能自動的將Integer[]轉(zhuǎn)成Object[]
Object obj = nums; //Integer[]當然是一個Object
int[] ids = {1, 2, 3, 4};
//Object[] objs2 = ids; //這里不能將int[]轉(zhuǎn)換成Object[]
Object obj2 = ids; //int[] 是一個Object
上面的例子表明:基本類型的一維數(shù)組只能當做Object,而不能當作Object[]。
int[][] intArray = {{1, 2}, {3, 4}};
Object[] oa = intArray;
Object obj = intArray;
//Integer[][] integerArray = intArray; int[][] 不是 Integer[][]
Integer[][] integerArray2 = new Integer[][]{{1, 2}, {3, 4}};
Object[][] oa2 = integerArray2;
Object[] oa3 = integerArray2;
Object obj2 = integerArray2;
從上面的例子可以看出java的二位數(shù)組是數(shù)組的數(shù)組。下面來看下對數(shù)組進行反射的例子:
package cn.zq.array.reflect;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
public class ArrayReflect {
public static void main(String[] args) {
Random rand = new Random(47);
int[] is = new int[10];
for(int i = 0; i < is.length; i++) {
is[i] = rand.nextInt(100);
}
System.out.println(is);
System.out.println(Arrays.asList(is));
/*以上的2個輸出都是輸出類似"[[I@14318bb]"的字符串,
不能顯示數(shù)組內(nèi)存放的內(nèi)容,當然我們采用遍歷的方式來輸出數(shù)組內(nèi)的內(nèi)容*/
System.out.println("--1.通過常規(guī)方式遍歷數(shù)組對數(shù)組進行打印--");
for(int i = 0; i < is.length; i++) {
System.out.print(is[i] + " ");
}
System.out.println();
System.out.println("--2.通過數(shù)組反射的方式遍歷數(shù)組對數(shù)組進行打印--");
Object obj = is; //將一維的int數(shù)組向上轉(zhuǎn)為Object
System.out.println("obj isArray:" + obj.getClass().isArray());
for(int i = 0; i < Array.getLength(obj); i++) {
int num = Array.getInt(obj, i);
//也能通過這個常用的方法來獲取對應索引位置的值
//Object value = Array.get(obj, i); //如果數(shù)組存放的是基本類型,那么返回的是基本類型對應的包裝類型
System.out.print(num + " ");
}
}
}
輸出:
[I@14318bb [[I@14318bb] --1.通過常規(guī)方式遍歷數(shù)組對數(shù)組進行打印-- 58 55 93 61 61 29 68 0 22 7 --2.通過數(shù)組反射的方式遍歷數(shù)組對數(shù)組進行打印-- obj isArray:true 58 55 93 61 61 29 68 0 22 7
上面的例子首先創(chuàng)建了一個int的一維數(shù)組,然后隨機的像里面填充0~100的整數(shù),接著通過System.out.println()方法直接對數(shù)組輸出或者用Arrays.asList方法(如果不是基本類型的一維數(shù)組此方法能按照期望轉(zhuǎn)成List,如果是二維數(shù)組也不能按照我們期望轉(zhuǎn)成List)將數(shù)組轉(zhuǎn)成List再輸出,通過都不是我們期望的輸出結(jié)果。接下來以常規(guī)的數(shù)組的遍歷方式來輸出數(shù)組內(nèi)的內(nèi)容,然后將int[]看成是一個Object,利用反射來遍歷其內(nèi)容。Class.isArray()可以用來判斷是對象是否為一個數(shù)組,假如是一個數(shù)組,那么在通過java.lang.reflect.Array這個對數(shù)組反射的工具類來獲取數(shù)組的相關(guān)信息,這個類通過了一些get方法,可以用來獲取數(shù)組的長度,各個版本的用來獲取基本類型的一維數(shù)組的對應索引的值,通用獲取值的方法get(Object array, int index),設置值的方法,還有2個用來創(chuàng)建數(shù)組實例的方法。通過數(shù)組反射工具類,可以很方便的利用數(shù)組反射寫出通用的代碼,而不用再去判斷給定的數(shù)組到底是那種基本類型的數(shù)組。
package cn.zq.array.reflect;
import java.lang.reflect.Array;
public class NewArrayInstance {
public static void main(String[] args) {
Object o = Array.newInstance(int.class, 20);
int[] is = (int[]) o;
System.out.println("is.length = " + is.length);
Object o2 = Array.newInstance(int.class, 10, 8);
int[][] iss = (int[][]) o2;
System.out.println("iss.length = " + iss.length
+ ", iss[0].lenght = " + iss[0].length);
}
}
is.length = 20
iss.length = 10, iss[0].lenght = 8
Array總共通過了2個方法來創(chuàng)建數(shù)組
Object newInstance(Class<?> componentType, int length),根據(jù)提供的class來創(chuàng)建一個指定長度的數(shù)組,如果像上面那樣提供int.class,長度為10,相當于new int[10];
Object newInstance(Class<?> componentType, int... dimensions),根據(jù)提供的class和維度來創(chuàng)建數(shù)組,可變參數(shù)dimensions用來指定數(shù)組的每一維的長度,像上面的例子那樣相當于創(chuàng)建了一個new int[10][8]的二維數(shù)組,但是不能創(chuàng)建每一維長度都不同的多維數(shù)組。通過第一種創(chuàng)建數(shù)組的方法,可以像這樣創(chuàng)建數(shù)組Object o = Array.newInstance(int[].class, 20)可以用來創(chuàng)建二維數(shù)組,這里相當于Object o = new int[20][];
當然通過上面例子那樣來創(chuàng)建數(shù)組的用法是很少見的,其實也是多余的,為什么不直接通過new來創(chuàng)建數(shù)組呢?反射創(chuàng)建數(shù)組不僅速度沒有new快,而且寫的程序也不易讀,還不如new來的直接。事實上通過反射創(chuàng)建數(shù)組確實很少見,是有何種變態(tài)的需求需要用反射來創(chuàng)建數(shù)組呢!
由于前面對基本類型的數(shù)組進行輸出時遇到一些障礙,下面將利用數(shù)組反射來實現(xiàn)一個工具類來實現(xiàn)期望的輸出:
package cn.zq.util;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Array;
public class Print {
public static void print(Object obj) {
print(obj, System.out);
}
public static void print(Object obj, PrintStream out) {
out.println(getPrintString(obj));
}
public static void println() {
print(System.out);
}
public static void println(PrintStream out) {
out.println();
}
public static void printnb(Object obj) {
printnb(obj, System.out);
}
public static void printnb(Object obj, PrintStream out) {
out.print(getPrintString(obj));
}
public static PrintStream format(String format, Object ... objects) {
return format(System.out, format, objects);
}
public static PrintStream format(PrintStream out, String format, Object ... objects) {
Object[] handleObjects = new Object[objects.length];
for(int i = 0; i < objects.length; i++) {
Object object = objects[i];
if(object == null || isPrimitiveWrapper(object)) {
handleObjects[i] = object;
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bos);
printnb(object, ps);
ps.close();
handleObjects[i] = new String(bos.toByteArray());
}
}
out.format(format, handleObjects);
return out;
}
/**
* 判斷給定對象是否為基本類型的包裝類。
* @param o 給定的Object對象
* @return 如果是基本類型的包裝類,則返回是,否則返回否。
*/
private static boolean isPrimitiveWrapper(Object o) {
return o instanceof Void || o instanceof Boolean
|| o instanceof Character || o instanceof Byte
|| o instanceof Short || o instanceof Integer
|| o instanceof Long || o instanceof Float
|| o instanceof Double;
}
public static String getPrintString(Object obj) {
StringBuilder result = new StringBuilder();
if(obj != null && obj.getClass().isArray()) {
result.append("[");
int len = Array.getLength(obj);
for(int i = 0; i < len; i++) {
Object value = Array.get(obj, i);
result.append(getPrintString(value));
if(i != len - 1) {
result.append(", ");
}
}
result.append("]");
} else {
result.append(String.valueOf(obj));
}
return result.toString();
}
}
上面的Print工具類提供了一些實用的進行輸出的靜態(tài)方法,并且提供了一些重載版本,可以根據(jù)個人的喜歡自己編寫一些重載的版本,支持基本類型的一維數(shù)組的打印以及多維數(shù)組的打印,看下下面的Print工具進行測試的示例:
package cn.zq.array.reflect;
import static cn.zq.util.Print.print;
import java.io.PrintStream;
import static cn.zq.util.Print.*;
public class PrintTest {
static class Person {
private static int counter;
private final int id = counter ++;
public String toString() {
return getClass().getSimpleName() + id;
}
}
public static void main(String[] args) throws Exception {
print("--打印非數(shù)組--");
print(new Object());
print("--打印基本類型的一維數(shù)組--");
int[] is = new int[]{1, 22, 31, 44, 21, 33, 65};
print(is);
print("--打印基本類型的二維數(shù)組--");
int[][] iss = new int[][]{
{11, 12, 13, 14},
{21, 22,},
{31, 32, 33}
};
print(iss);
print("--打印非基本類型的一維數(shù)組--");
Person[] persons = new Person[10];
for(int i = 0; i < persons.length; i++) {
persons[i] = new Person();
}
print(persons);
print("--打印非基本類型的二維數(shù)組--");
Person[][] persons2 = new Person[][]{
{new Person()},
{new Person(), new Person()},
{new Person(), new Person(), new Person(),},
};
print(persons2);
print("--打印empty數(shù)組--");
print(new int[]{});
print("--打印含有null值的數(shù)組--");
Object[] objects = new Object[]{
new Person(), null, new Object(), new Integer(100)
};
print(objects);
print("--打印特殊情況的二維數(shù)組--");
Object[][] objects2 = new Object[3][];
objects2[0] = new Object[]{};
objects2[2] = objects;
print(objects2);
print("--將一維數(shù)組的結(jié)果輸出到文件--");
PrintStream out = new PrintStream("out.c");
try {
print(iss, out);
} finally {
out.close();
}
print("--格式化輸出--");
format("%-6d%s %B %s", 10086, "is", true, iss);
/**
* 上面列出了一些Print工具類的一些常用的方法,
* 還有一些未列出的方法,請自行查看。
*/
}
}
輸出:
--打印非數(shù)組-- java.lang.Object@61de33 --打印基本類型的一維數(shù)組-- [1, 22, 31, 44, 21, 33, 65] --打印基本類型的二維數(shù)組-- [[11, 12, 13, 14], [21, 22], [31, 32, 33]] --打印非基本類型的一維數(shù)組-- [Person0, Person1, Person2, Person3, Person4, Person5, Person6, Person7, Person8, Person9] --打印非基本類型的二維數(shù)組-- [[Person10], [Person11, Person12], [Person13, Person14, Person15]] --打印empty數(shù)組-- [] --打印含有null值的數(shù)組-- [Person16, null, java.lang.Object@ca0b6, 100] --打印特殊情況的二維數(shù)組-- [[], null, [Person16, null, java.lang.Object@ca0b6, 100]] --將一維數(shù)組的結(jié)果輸出到文件-- --格式化輸出-- 10086 is TRUE [[11, 12, 13, 14], [21, 22], [31, 32, 33]]
輸出文件:

可見Print工具類已經(jīng)具備打印基本類型的一維數(shù)組以及多維數(shù)組的能力了,總體來說上面的工具類還是挺實用的,免得每次想要看數(shù)組里面的內(nèi)容都有手動的去編寫代碼,那樣是在是太麻煩了,以后直接把Print工具類拿過去用就行了,多么的方便啊。
上面的工具類確實能很好的工作,但是假如有這樣一個需求:給你一個數(shù)組(也有可能是其他的容器),你給我整出一個List。那么我們應該怎樣做呢?事實上Arrays.asList不總是能得到我們所期望的結(jié)果,java5雖然添加了泛型,但是是有限制的,并不能像c++的模板那樣通用,正是因為java中存在基本類型,即使有自動包裝的機制,與泛型一起并不能使用,參數(shù)類型必須是某種類型,而不能是基本類型。下面給出一種自己的解決辦法:
package cn.zq.util;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class CollectionUtils {
public static List<?> asList(Object obj) {
return convertToList(
makeIterator(obj));
}
public static <T>List<T> convertToList(Iterator<T> iterator) {
if(iterator == null) {
return null;
}
List<T> list = new ArrayList<T>();
while(iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Iterator<?> makeIterator(Object obj) {
if(obj instanceof Iterator) {
return (Iterator<?>) obj;
}
if(obj == null) {
return null;
}
if(obj instanceof Map) {
obj = ((Map<?, ?>)obj).entrySet();
}
Iterator<?> iterator = null;
if(obj instanceof Iterable) {
iterator = ((Iterable<?>)obj).iterator();
} else if(obj.getClass().isArray()) {
//Object[] objs = (Object[]) obj; //原始類型的一位數(shù)組不能這樣轉(zhuǎn)換
ArrayList list = new ArrayList(Array.getLength(obj));
for(int i = 0; i < Array.getLength(obj); i++) {
list.add(Array.get(obj, i));
}
iterator = list.iterator();
} else if(obj instanceof Enumeration) {
iterator = new EnumerationIterator((Enumeration) obj);
} else {
iterator = Arrays.asList(obj).iterator();
}
return iterator;
}
public static class EnumerationIterator<T> implements Iterator<T> {
private Enumeration<T> enumeration;
public EnumerationIterator(Enumeration<T> enumeration) {
this.enumeration = enumeration;
}
public boolean hasNext() {
return enumeration.hasMoreElements();
}
public T next() {
return enumeration.nextElement();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
測試代碼:
package cn.zq.array.reflect;
import java.util.Iterator;
import java.util.List;
import cn.zq.array.reflect.PrintTest.Person;
import cn.zq.util.CollectionUtils;
public class CollectionUtilsTest {
public static void main(String[] args) {
System.out.println("--基本類型一維數(shù)組--");
int[] nums = {1, 3, 5, 7, 9};
List<?> list = CollectionUtils.asList(nums);
System.out.println(list);
System.out.println("--非基本類型一維數(shù)組--");
Person[] persons = new Person[]{
new Person(),
new Person(),
new Person(),
};
List<Person> personList = (List<Person>) CollectionUtils.asList(persons);
System.out.println(personList);
System.out.println("--Iterator--");
Iterator<Person> iterator = personList.iterator();
List<Person> personList2 = (List<Person>) CollectionUtils.asList(iterator);
System.out.println(personList2);
}
}
輸出:
--基本類型一維數(shù)組-- [1, 3, 5, 7, 9] --非基本類型一維數(shù)組-- [Person0, Person1, Person2] --Iterator-- [Person0, Person1, Person2]
在java的容器類庫中可以分為Collection,Map,數(shù)組,由于Iterator(以及早期的遺留接口Enumeration)是所有容器的通用接口并且Collection接口從Iterable(該接口的iterator將返回一個Iterator),所以在makeIterator方法中對這些情形進行了一一的處理,對Map類型,只需要調(diào)用其entrySet()方法,對于實現(xiàn)了Iterable接口的類(Collection包含在內(nèi)),調(diào)用iterator()直接得到Iterator對象,對于Enumeration類型,利用適配器EnumerationIterator進行適配,對于數(shù)組,利用數(shù)組反射遍歷數(shù)組放入ArrayList中,對于其他的類型調(diào)用Arrays.asList()方法創(chuàng)建一個List。CollectionUtils還提供了一些其他的方法來進行轉(zhuǎn)換,可以根據(jù)需要添加自己需要的方法。
總結(jié):數(shù)組的反射對于那些可能出現(xiàn)數(shù)組的設計中提供更方便、更靈活的方法,以免寫那些比較麻煩的判斷語句,這種靈活性付出的就是性能的代價,對于那些根本不需要數(shù)組反射的情況下用數(shù)組的反射實在是不應該。是否使用數(shù)組的反射,在實際的開發(fā)中仁者見仁智者見智,根據(jù)需要來選擇是否使用數(shù)組的反射,最好的方式就是用實踐來探路,先按照自己想到的方式去寫,在實踐中不斷的完善。
相關(guān)文章
Java數(shù)字轉(zhuǎn)中文大寫工具類詳細代碼(拿去即用)
最近項目中用到金額轉(zhuǎn)大寫的地方,索性給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于Java數(shù)字轉(zhuǎn)中文大寫工具類的相關(guān)資料,文中給出了詳細的代碼示例,需要的朋友可以參考下2024-05-05
Java 實現(xiàn)完整功能的學生管理系統(tǒng)實例
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實現(xiàn)一個完整版學生管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
springboot創(chuàng)建多module項目的實例
這篇文章主要介紹了springboot創(chuàng)建多module項目的實例代碼,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
如何將JSP/Servlet項目轉(zhuǎn)換為Spring Boot項目
這篇文章主要介紹了如何將JSP/Servlet項目轉(zhuǎn)換為Spring Boot項目,幫助大家更好的利用springboot進行網(wǎng)絡編程,感興趣的朋友可以了解下2020-10-10
使用google.kaptcha來生成圖片驗證碼的實現(xiàn)方法
這篇文章主要介紹了使用google.kaptcha來生成圖片驗證碼的實現(xiàn)方法,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09
java線程池對象ThreadPoolExecutor的深入講解
在我們的開發(fā)中“池”的概念并不罕見,有數(shù)據(jù)庫連接池、線程池、對象池、常量池等等。下面這篇文章主要給大家介紹了關(guān)于java線程池對象ThreadPoolExecutor的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧2018-09-09

