Java 直方图放大问题
我已经解决了编译问题,并且遇到了使用升迁直方图方法的问题。由于某种原因,我的方法“复制”部分似乎不起作用。有什么想法吗?我会在代码中标记它 以下是我编写的全部代码Java 直方图放大问题,java,methods,compiler-errors,histogram,Java,Methods,Compiler Errors,Histogram,我已经解决了编译问题,并且遇到了使用升迁直方图方法的问题。由于某种原因,我的方法“复制”部分似乎不起作用。有什么想法吗?我会在代码中标记它 以下是我编写的全部代码 import java.io.*; import java.util.*; public class Project5 { static final int INITIAL_CAPACITY = 10; public static void main (String[] args) throws Exception
import java.io.*;
import java.util.*;
public class Project5
{
static final int INITIAL_CAPACITY = 10;
public static void main (String[] args) throws Exception
{
if (args.length < 1) die("You must type the dictionary filename on cmd line.\n");
// Here we have declared an int array, called 'histogram' with initial capacity of 0
// it is a freq counter to word lengths in the file
int[] histogram = new int[0];
// Here we have declared an array of String to read the dictionary file into. We use BufferedReader (not Scanner).
// With each word read in, examine it's length and update word length frequency histogram accordingly
String[] wordList = new String[INITIAL_CAPACITY];
int wordCount = 0;
BufferedReader infile = new BufferedReader( new FileReader(args[0]) );
while ( infile.ready() ) // i.e. while there are more lines of text in the file
{
String word = infile.readLine();
// YOUR CODE HERE TO CHECK TO SEE IF WORDLIST IS FULL
// IF SO YOU MUST DO AN UPSIZE JUST LIKE LAB#6
if ( wordCount == wordList.length )
wordList = upSizeArr( wordList );
// YOUR CODE HERE to add this word to your list
String newWord = infile.next();
wordList[wordCount++] = newWord;
// YOUR CODE HERE TO LOOK AT THE LENGTH OF THE WORD AND UPDATE HISTOGRAM
int wordLength = word.length;
if (word.length() > histogram.length)
histogram = upSizeHisto( histogram, wordLength );
histogram[word.length()]++;
// example if word.length() is 5 then histogram[5] gets increment
// BUT IF WORD LENGTH IS >= HISTORGRAM LENGTH
// THEN YOU NEED TO FIRST CALL upSizeHisto TO UPSIZE THE HISTOGRAM TO BE OF EXACTLY LENGTH word.length()+1
// SIMILAR TO HOW YOU HAD TO UPSIZE WORDLIST
} //END WHILE INFILE READY
infile.close();
wordList = trimArr( wordList, wordCount );
System.out.println( "After trim, wordList length: " + wordList.length );
// PRINT WORD LENGTH FREQ HISTOGRAM
for ( int i = 0; i < histogram.length ; i++ )
System.out.println("words of length " + i + ": " + histogram[i]);
} // END main
private static void die( String msg )
{
System.out.println( msg );
System.exit(0);
}
private static String[] upSizeArr( String[] oldArr )
{
String[] newArr = new String[oldArr.length * 2];
for (int cnt = 0; cnt < oldArr.length; cnt++)
{
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static String[] trimArr( String[] oldArr, int count )
{
String[] newArr = new String[count];
for (int cnt = 0; cnt < newArr.length; cnt++)
{
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static int[] upSizeHisto( int[] oldArr, int newLength )
{
int[] newHisto = new int[newLength];
for (int cnt = 0; cnt < newHisto.length; cnt++)
{
newHisto[cnt] = oldArr[cnt]; // ERROR HERE, Any help guys?
}
return newHisto;
}
} // END CLASS PROJECT#5
import java.io.*;
导入java.util.*;
公共类项目5
{
静态最终int初始容量=10;
公共静态void main(字符串[]args)引发异常
{
如果(args.length<1)死亡(“必须在cmd行键入字典文件名。\n”);
//这里我们声明了一个int数组,称为“直方图”,初始容量为0
//它是文件中字长的频率计数器
int[]直方图=新的int[0];
//这里我们声明了一个字符串数组,用于将字典文件读入其中。我们使用BufferedReader(而不是Scanner)。
//读入每个单词后,检查其长度并相应地更新单词长度频率直方图
String[]wordList=新字符串[初始容量];
int字数=0;
BufferedReader infle=new BufferedReader(new FileReader(args[0]));
while(infle.ready())//即当文件中有更多的文本行时
{
String word=infle.readLine();
//您的代码将在此处检查单词列表是否已满
//如果是这样的话,你必须做一个像6号实验室一样的大型化
if(wordCount==wordList.length)
wordList=upSizeArr(wordList);
//请在此处输入代码,以便将此单词添加到列表中
字符串newWord=infle.next();
wordList[wordCount++]=新词;
//您的代码在这里查看单词的长度并更新直方图
int wordLength=word.length;
if(word.length()>histogram.length)
直方图=放大直方图(直方图,字长);
直方图[word.length()]+;
//例如,如果word.length()为5,则直方图[5]将获得增量
//但是如果单词长度>=直方图长度
//然后,您需要首先调用upSizeHisto,将直方图升迁为精确长度word.LENGTH()+1
//类似于您必须放大单词列表的方式
}//在填充就绪时结束
infle.close();
wordList=trimArr(wordList,wordCount);
System.out.println(“修剪后,单词列表长度:“+wordList.length”);
//打印字长频率直方图
对于(int i=0;i
我可以使用的最后一点帮助是代码中的最后一个方法。由于错误,我还没有测试它,但是如果有任何突出的问题,我可以使用帮助
谢谢大家。很少出错
1.)
到
(二)
到
3.)添加Try-Catch
代码如下
public class Project5 {
static final int INITIAL_CAPACITY = 10;
public static void main(String[] args) {
if (args.length < 1)
die("You must type the dictionary filename on cmd line.\n");
// Here we have declared an int array, called 'histogram' with initial capacity of 0
// it is a freq counter to word lengths in the file
int[] histogram = new int[0];
// Here we have declared an array of String to read the dictionary file into. We use BufferedReader (not Scanner).
// With each word read in, examine it's length and update word length frequency histogram accordingly
String[] wordList = new String[INITIAL_CAPACITY];
int wordCount = 0;
try {
BufferedReader infile = new BufferedReader(new FileReader(args[0]));
while (infile.ready()) // i.e. while there are more lines of text in the file
{
String word = infile.readLine();
// YOUR CODE HERE TO CHECK TO SEE IF WORDLIST IS FULL
// IF SO YOU MUST DO AN UPSIZE JUST LIKE LAB#6
if (wordCount == wordList.length)
wordList = upSizeArr(wordList);
// YOUR CODE HERE to add this word to your list
String newWord = infile.readLine();
wordList[wordCount++] = newWord;
// YOUR CODE HERE TO LOOK AT THE LENGTH OF THE WORD AND UPDATE HISTOGRAM
int wordLength = word.length();
if (word.length() > histogram.length)
histogram = upSizeHisto(histogram, wordLength);
histogram[word.length()]++;
// example if word.length() is 5 then histogram[5] gets increment
// BUT IF WORD LENGTH IS >= HISTORGRAM LENGTH
// THEN YOU NEED TO FIRST CALL upSizeHisto TO UPSIZE THE HISTOGRAM TO BE OF EXACTLY LENGTH word.length()+1
// SIMILAR TO HOW YOU HAD TO UPSIZE WORDLIST
infile.close();
} // END WHILE INFILE READY
} catch (Exception e) {
}
wordList = trimArr(wordList, wordCount);
System.out.println("After trim, wordList length: " + wordList.length);
// PRINT WORD LENGTH FREQ HISTOGRAM
for (int i = 0 ; i < histogram.length ; i++)
System.out.println("words of length " + i + ": " + histogram[i]);
} // END main
private static void die(String msg) {
System.out.println(msg);
System.exit(0);
}
private static String[] upSizeArr(String[] oldArr) {
String[] newArr = new String[oldArr.length * 2];
for (int cnt = 0 ; cnt < oldArr.length ; cnt++) {
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static String[] trimArr(String[] oldArr, int count) {
String[] newArr = new String[count];
for (int cnt = 0 ; cnt < newArr.length ; cnt++) {
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static int[] upSizeHisto(int[] oldArr, int newLength) {
int[] newHisto = new int[newLength];
for (int cnt = 0 ; cnt < newHisto.length ; cnt++) {
newHisto[cnt] = oldArr[cnt];
}
return newHisto;
}
}
公共类项目5{
静态最终int初始容量=10;
公共静态void main(字符串[]args){
如果(参数长度<1)
die(“必须在cmd行键入字典文件名。\n”);
//这里我们声明了一个int数组,称为“直方图”,初始容量为0
//它是文件中字长的频率计数器
int[]直方图=新的int[0];
//这里我们声明了一个字符串数组,用于将字典文件读入其中。我们使用BufferedReader(而不是Scanner)。
//读入每个单词后,检查其长度并相应地更新单词长度频率直方图
String[]wordList=新字符串[初始容量];
int字数=0;
试一试{
BufferedReader infle=new BufferedReader(new FileReader(args[0]));
while(infle.ready())//即当文件中有更多的文本行时
{
String word=infle.readLine();
//您的代码将在此处检查单词列表是否已满
//如果是这样的话,你必须做一个像6号实验室一样的大型化
if(wordCount==wordList.length)
wordList=upSizeArr(wordList);
//请在此处输入代码,以便将此单词添加到列表中
字符串newWord=infle.readLine();
wordList[wordCount++]=新词;
//您的代码在这里查看单词的长度并更新直方图
int wordLength=word.length();
if(word.length()>histogram.length)
直方图=放大直方图(直方图,字长);
直方图[word.length()]+;
//例如,如果word.length()为5,则直方图[5]将获得增量
//但是如果单词长度>=直方图长度
//然后,您需要首先调用upSizeHisto,将直方图升迁为精确长度word.LENGTH()+1
//类似于您必须放大单词列表的方式
infle.close();
}//在填充就绪时结束
}捕获(例外e){
}
wordList=trimArr(wordList,wordCount);
System.out.println(“修剪后,字表长度:“+wordList.length”);
//打印字长
int wordLength = word.length();
String newWord = infile.next();
String newWord = infile.readLine();
public class Project5 {
static final int INITIAL_CAPACITY = 10;
public static void main(String[] args) {
if (args.length < 1)
die("You must type the dictionary filename on cmd line.\n");
// Here we have declared an int array, called 'histogram' with initial capacity of 0
// it is a freq counter to word lengths in the file
int[] histogram = new int[0];
// Here we have declared an array of String to read the dictionary file into. We use BufferedReader (not Scanner).
// With each word read in, examine it's length and update word length frequency histogram accordingly
String[] wordList = new String[INITIAL_CAPACITY];
int wordCount = 0;
try {
BufferedReader infile = new BufferedReader(new FileReader(args[0]));
while (infile.ready()) // i.e. while there are more lines of text in the file
{
String word = infile.readLine();
// YOUR CODE HERE TO CHECK TO SEE IF WORDLIST IS FULL
// IF SO YOU MUST DO AN UPSIZE JUST LIKE LAB#6
if (wordCount == wordList.length)
wordList = upSizeArr(wordList);
// YOUR CODE HERE to add this word to your list
String newWord = infile.readLine();
wordList[wordCount++] = newWord;
// YOUR CODE HERE TO LOOK AT THE LENGTH OF THE WORD AND UPDATE HISTOGRAM
int wordLength = word.length();
if (word.length() > histogram.length)
histogram = upSizeHisto(histogram, wordLength);
histogram[word.length()]++;
// example if word.length() is 5 then histogram[5] gets increment
// BUT IF WORD LENGTH IS >= HISTORGRAM LENGTH
// THEN YOU NEED TO FIRST CALL upSizeHisto TO UPSIZE THE HISTOGRAM TO BE OF EXACTLY LENGTH word.length()+1
// SIMILAR TO HOW YOU HAD TO UPSIZE WORDLIST
infile.close();
} // END WHILE INFILE READY
} catch (Exception e) {
}
wordList = trimArr(wordList, wordCount);
System.out.println("After trim, wordList length: " + wordList.length);
// PRINT WORD LENGTH FREQ HISTOGRAM
for (int i = 0 ; i < histogram.length ; i++)
System.out.println("words of length " + i + ": " + histogram[i]);
} // END main
private static void die(String msg) {
System.out.println(msg);
System.exit(0);
}
private static String[] upSizeArr(String[] oldArr) {
String[] newArr = new String[oldArr.length * 2];
for (int cnt = 0 ; cnt < oldArr.length ; cnt++) {
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static String[] trimArr(String[] oldArr, int count) {
String[] newArr = new String[count];
for (int cnt = 0 ; cnt < newArr.length ; cnt++) {
newArr[cnt] = oldArr[cnt];
}
return newArr;
}
private static int[] upSizeHisto(int[] oldArr, int newLength) {
int[] newHisto = new int[newLength];
for (int cnt = 0 ; cnt < newHisto.length ; cnt++) {
newHisto[cnt] = oldArr[cnt];
}
return newHisto;
}
}