日期:2014-05-20  浏览次数:20757 次

关于扩充数组的问题
我看书上写的一个可以扩充数组容量的程序,不过为什么运行时出现问题呢?
程序如下:
import   java.lang.Class;
import   java.lang.reflect.*;
public   class   ArrayGrow   {
  static   Object   arraygrow(Object[]   a){
Class   cl=a.getClass();
if(!cl.isArray())return   null;
Class   componentType=a.getClass().getComponentType();
int   length=Array.getLength(a);
int   newlength=length*11/10+10;
Object   newArray=Array.newInstance(componentType,   newlength);
System.arraycopy(a,   0,   newArray,   0,   length);
return(newArray);
}  
public   static   void   main(String[]   args){
int[]   a={1,2,3,4};
a=(int[])arraygrow(a);|||||||||||||||
for(int   i=0;i <a.length;i++)
System.out.println( "a[ "+i+ "] "+ "= "+a[i]);
}
}
在打|||||||||||||||的地方出错
提示出错:
The   method   arraygrow(Object[])   in   the   type   ArrayGrow   is   not   applicable   for   the  
  arguments   (int[])
谢谢高手们指教

------解决方案--------------------
public static void main(String[] args) {
Integer[] a = new Integer[]{ 1, 2, 3, 4 };
Integer[] b = (Integer[])arraygrow(a);
for (int i = 0; i < b.length; i++)
System.out.println( "a[ " + i + "] " + "= " + a[i]);
}
int是基本数据类型,要用包装类。
但我没看你程序,不知道具体你要干什么。