日期:2014-05-18  浏览次数:20658 次

一个小小的问题
package   com.accp;

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

/**
  *   <p> Title:   </p>
  *
  *   <p> Description:   </p>
  *
  *   <p> Copyright:   Copyright   (c)   2007 </p>
  *
  *   <p> Company:   </p>
  *
  *   @author   not   attributable
  *   @version   1.0
  */
public   class   ArrayExample   {
        public   ArrayExample()   {
        }

        public   static   void   main(String[]   args)   {
                ArrayExample   examp   =   new   ArrayExample();
                int   number[]   =new   int[]   {1,   2,   3,   4,   5,   6};
                Object   arr   =   Array.newInstance(Integer.TYPE,   number);
                Object   obj   =   Array.get(arr,   3);
                Class   cla=obj.getClass().getComponentType();
                System.out.println( " "   +   cla);
        }
}
为什么编译器会报数组越界的异常
我明明实例化了的啊

------解决方案--------------------
对Array.newInstance的理解有问题,Array.newInstance()的原型是:
public static Object newInstance(Class <?> componentType,
int[] dimensions);

你看,第二个参数是dimensions,也就是说你创建一个多维数组:

int[1][2][3][4][5][6]

所以,arr数组的长度是1,你自能执行: Array.get( arr,0 )