Mega Code Archive

 
Categories / Java Tutorial / Collections
 

Using reflection to check array type and length

import java.lang.reflect.Array; public class MainClass {   public static void main (String args[]) {     int[] object = {1,2,3};     Class type = object.getClass();     if (type.isArray()) {       Class elementType = type.getComponentType();       System.out.println("Array of: " + elementType);       System.out.println("Length: " + Array.getLength(object));     }   } } Array of: int Length: 3