Reflection of array

Keywords: Java

1, Overview

1. Arrays with the same dimension and element type belong to the same type, that is, they have the same Class instance object.

2. The getSuperClass() method of the Class instance Object representing the array returns the parent Class corresponding to the Object Class.

3. One dimensional array of basic type can be used as Object type, not as Object [] type; one dimensional array of non basic type can be used as both Object type and Object [] type.

4. The differences between the Arrays.asList() method when it processes int [] and String [].

5. The Array tool class is used to complete the reflection operation of the Array.

java.reflect.Array gets the array length, get value, set value, etc.

//Set value to array
	private static void setArrayReflectApp(Object obj) {
		if(obj.getClass().isArray()){
			for(int i = 0; i < Array.getLength(obj); i++){
				Array.set(obj, i, i);
			}
		}
	}

	//Print an array or object
	private static void arrayReflectApp(Object obj) {
		if(obj.getClass().isArray()){
			for(int i = 0; i < Array.getLength(obj); i++){
				System.out.println(Array.get(obj, i));
			}
		}
		else {
			System.out.println(obj);
		}
	}

6. Thinking question: how to get the element type in the array?

You can only get the specific type of an element:

Object[] obj =new  Object[]{1,"abc"};
obj[i].getClass().getName();

2, Code description

    ArrayReflectTest.java

package staticimport.reflect;

import java.lang.reflect.Array;
import java.util.Arrays;

/***
 * 
 * The relation between array and Object and its reflection type
 * 
 * Reflection application of array
 * 
 * @author Liu
 *
 */
public class ArrayReflectTest {

	public static void main(String[] args) {
		int[] a1 = new int[]{1,2,3};
		int[] a2 = new int[4];
		int[][] a3 = new int[2][3];
		int[][] a33 = new int[][]{new int[]{1,2},new int[]{3,4}};
		int[][] a333 = {{1,2,3},{4,5,6},{7,8,9}};
		
		
		String[] a4 = new String[]{"a","b","c"};
		
		System.out.println(a1.getClass() == a2.getClass());//true
//		System.out.println(a1.getClass() == a4.getClass());//false
//		System.out.println(a1.getClass() == a3.getClass());//false
		
		System.out.println(a1.getClass().getName());
		System.out.println(a1.getClass().getSuperclass().getName());
		System.out.println(a4.getClass().getSuperclass().getName());
		
		Object obj1 = a1;
		Object obj2 = a4;
//		Object [] obb3 = A1; / / the parent classes of one-dimensional array of basic types are all objects
		Object[] obj4 = a3;
		Object[] obj5 = a4;
		
		System.out.println(a1);
		System.out.println(a4);
		
		//The parent class of int [] is Object type instead of Object []
		//Try using the jdk1.4 syntax first, and find out that it can't be done, and then use the jdk1.5 syntax
		System.out.println(Arrays.asList(a1));//[[I@15db9742]
		
		//The parent class of String [] is Object type as well as Object []
		//It is found that using jdk1.4 syntax can
		System.out.println(Arrays.asList(a4));//[a, b, c]
		
		//Two dimensional array
		System.out.println(Arrays.asList(a33));
		
		//Two dimensional array
		System.out.println(Arrays.asList(a333));
		
		//Reflection application of array
		arrayReflectApp(a1);
		arrayReflectApp(a4);
		arrayReflectApp("abc");
		
		setArrayReflectApp(a1);
		
		arrayReflectApp(a1);
	}

	//Set value to array
	private static void setArrayReflectApp(Object obj) {
		if(obj.getClass().isArray()){
			for(int i = 0; i < Array.getLength(obj); i++){
				Array.set(obj, i, i);
			}
		}
	}

	//Print an array or object
	private static void arrayReflectApp(Object obj) {
		if(obj.getClass().isArray()){
			for(int i = 0; i < Array.getLength(obj); i++){
				System.out.println(Array.get(obj, i));
			}
		}
		else {
			System.out.println(obj);
		}
	}

}

Posted by niroshana35 on Thu, 30 Apr 2020 06:38:01 -0700