public class Letters extends Component { long[] countArray = new long[26]; static char[] letterArray = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', 'p','q','r','s','t','u','v','w','x','y','z'}; /** * Find the number of occurrences of each letter of the * alphabet in the named file. The result is returned * as a 26-element array of long elements. * Of course, this will only work for the English alphabet. */ void countCharacters (String filename) throws IOException { System.out.PRintln ("...reading " + filename); FileInputStream fis = new FileInputStream (filename); int tmp; while ((tmp = fis.read()) != -1) { char c = Character.toLowerCase((char)tmp); int pos = c - 'a'; if ((pos >= 0) && (pos <= 25)) { ++countArray[pos]; } } fis.close(); }
/** * Draw a histogram of the letter frequency. * This method is triggered by repaint(), or by * window manager repaint events. */ public void paint (Graphics g) { long maxCount = 0; for (int i=0; i maxCount) maxCount = countArray[i]; } Dimension d = getSize(); double yScale = ((double)d.height) / ((double)maxCount); int barWidth = (int)d.width / countArray.length; int x = 0; for (int j=0; j 这个程序的问题究竟是什么?我们又该如何来解决?我们还是按照流程来进行吧 步骤1: 确定什么样的性能级别是足够好的 假如程序能在10秒左右的时间里完成读取和处理javax.swing 包中的Html文档,我自己就会武断的认为性能已经足够不错了。