读书人

java.lang.ArrayIndexOutOfBoundsExce

发布时间: 2011-11-14 22:03:59 作者: rapoo

新手读文件出错
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];
然后再试试

读书人网 >J2SE开发

热点推荐