首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java.lang.ArrayIndexOutOfBoundsException: -1

2011-11-14 
新手读文件出错classHaff_NodeimplementsSerializable{charchStringhcodeintlchildintrchildintfreqi

新手读文件出错
class   Haff_Node   implements   Serializable
{
char   ch;
String   hcode;
int   lchild;
int   rchild;
int   freq;
int   flag;
int   num;

}
我用这个方法存
//Haff_Tree是一个Haff_Node的数组
public   void   Save_Tree_to_Text()
{
try
{
ObjectOutputStream   out   =   new   ObjectOutputStream(new   FileOutputStream( "Haff_tree.dat "));

out.writeObject(Haff_Tree);
out.close();

System.out.println( "ok!save ");
}

catch(Exception   e)
{
e.printStackTrace();
}
}

但是用这个方法读的时候,什么都读不出来
public   void   Load_Text_to_Tree()
{
try
{
ObjectInputStream   in   =   new   ObjectInputStream(new   FileInputStream( "Haff_tree.dat "));
Haff_Node[]   Haff_Tree   =   (Haff_Node[])in.readObject();

in.close();

System.out.println( "ok!load ");
}
catch(Exception   e)
{
e.printStackTrace();
}
}


[解决办法]
java.lang.ArrayIndexOutOfBoundsException: -1这的不是说越界么?
[解决办法]
越界異常!
[解决办法]
Haff_Node[] Haff_Tree = (Haff_Node[])in.readObject();

Haff_Node[]的大小是多少呢?你把读到的值给这个赋值,Haff_Node没有指定大小,当然越界了
[解决办法]
Haff_Node[] Haff_Tree = new Haff_Node[1000];
然后再试试

热点排行