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

新人求助,报错java.lang.outOFmemortError
新人代码写的不规范

说明:程序主要是统计一个文件里面字符出现的频率,并保存在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++;
                  }
              }