大家来看一个很有趣的现象
import java.util.Hashtable;
public class TObjectArray
{
Hashtable table[];
int i[];
public static void main(String[] args)
{
TObjectArray object=new TObjectArray();
object.test1();
object.test2();
}
public void test1()
{
table=new Hashtable[1];
table[0]=new Hashtable();
table[0].put("1", "abc");
table=new Hashtable[2];
table[1]=new Hashtable();
table[1].put("2", "efg");
//System.out.println(table[0].get("1")); 这句错
System.out.println(table[1].get("2"));//这句没错
}
public void test2()
{
i=new int[1];
i[0]=0;
i=new int[2];
i[1]=1;
for (int j = 0; j < i.length; j++)
{
System.out.println(i[j]);
}
}
}
//上面的代码,是动态改变两种数组的维数,一种是基本类型的数组,一种是对象数组,发现,基本类型的数组,是可以动态改变维数,并保留原有的数值,但是对象型数组都不可以了,每次改变维数,原来的对象都会丢失。有人能解释一下吗?