新人求助,报错java.lang.outOFmemortError

民族疯 2014-04-19 07:40:13
新人代码写的不规范

说明:程序主要是统计一个文件里面字符出现的频率,并保存在ArrayList<Letter>里面

/////////////字符类/////////////////
public class Letter
{

private char charName; //字符名
private int times; //字符出现的次数
private double weight; //字符权值

public Letter()
{
times = 0;
weight = 0;
}

public char getCharName()
{
return charName;
}
public void setCharName(char charName)
{
this.charName = charName;
}
public int getTimes()
{
return times;
}
public void setTimes(int times)
{
this.times = times;
}
public double getWeight()
{
return weight;
}
public void setWeight(double weight)
{
this.weight = weight;
}
public double getTimesPlus()
{
return times++;
}

}


/////////////////////字符信息类/////////////////////
import java.util.ArrayList;
import java.util.Iterator;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.File;
public class LetterInformation
{
private int n; //字符的种类
private double sum; //字符的总数
private ArrayList<Letter> letters; //存储字母信息

public LetterInformation()
{
n = 0;
sum = 0.0;
letters = new ArrayList<Letter>();
letters.ensureCapacity(250);
}

public void getWeight() //获得各字符权值大小
{
for (int i = 0; i< letters.size() ; i++)
{
int t = letters.get(i).getTimes();
letters.get(i).setWeight(t/sum);
}
}

public void readLetter(File src) //初始化letters
{
try(
BufferedInputStream fr = new BufferedInputStream(new FileInputStream(src));
)
{
if (!fr.markSupported())
{
System.out.println("mark/reset not supported!");
}
int c = fr.read();
char ch = (char)c;
Letter temp = new Letter(); //临时存放字符
temp.setCharName(ch);
letters.add(temp); //读取文件的第一个字符
fr.mark(1);
fr.reset();
n++;
while ((c = fr.read())!= -1)
{
Letter tp = new Letter();
ch = (char)c;
int tag = 0;
for (int i = 0; i < letters.size(); i++)
{
if ((letters.get(i)).getCharName() == ch)
{
letters.get(i).getTimesPlus(); //相同字符就累加1
sum++;
tag = 1;
}
if (tag == 0) //不同的字符就存在链表中
{
n++;
tp.setCharName(ch);
tp.getTimesPlus();
letters.add(temp);
sum++;
}
}
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
getWeight();
}
public ArrayList<Letter> getLetters()
{
return letters;
}
public int getN()
{
return n;
}
public static void main(String[] args)
{
LetterInformation lif = new LetterInformation();
lif.readLetter(new File("test.txt"));
}
}


报错:


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2245)
at java.util.Arrays.copyOf(Arrays.java:2219)
at java.util.ArrayList.grow(ArrayList.java:242)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)
at java.util.ArrayList.add(ArrayList.java:440)
at LetterInformation.readLetter(LetterInformation.java:67)
at LetterInformation.main(LetterInformation.java:90)
请按任意键继续. . .



这是咋回事呢?

...全文
186 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Z-ero 2014-04-20
  • 打赏
  • 举报
回复
那就不清楚了,还是菜鸟一个.不过楼主可以手动关流试试,如果还是这样就不是关流的问题了
民族疯 2014-04-20
  • 打赏
  • 举报
回复
大神都去哪儿了?
民族疯 2014-04-20
  • 打赏
  • 举报
回复
求大神,求大神.....!!!!1
民族疯 2014-04-19
  • 打赏
  • 举报
回复
引用 2 楼 demonxiao 的回复:
LZ。。有文件流操作的话,记得close掉。你这个异常是内存溢出了。
java7中这些流都实现了AutoCloseable接口,只要创建时候放在try语句中就可以自动关闭的。。
民族疯 2014-04-19
  • 打赏
  • 举报
回复
引用 1 楼 u014547383 的回复:
内存溢出,这是没关流么?
java7中这些流都实现了AutoCloseable接口,只要创建时候放在try语句中就可以自动关闭的。。
Sean-Xiao 2014-04-19
  • 打赏
  • 举报
回复
LZ。。有文件流操作的话,记得close掉。你这个异常是内存溢出了。
Z-ero 2014-04-19
  • 打赏
  • 举报
回复
内存溢出,这是没关流么?

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧