我一直在嘗試升級Java技能以使用更多的Java 5 & Java 6。我一直在玩一些編程練習。我被要求從一個文本文件中讀取一段文字,並輸出一個排序(降序)的單詞列表並輸出每個單詞的計數。更高效還是更現代?用Java讀入和排序文本文件
我的代碼如下。
我的問題是:
是我的文件輸入程序最恭敬JVM的資源?
是否可以減少讀取文件內容和將內容導入可以創建排序列表的單詞的步驟?
我使用集合類和接口是最有效的方式嗎?
非常感謝您的意見。我只是想獲得一些樂趣並提高我的編程技能。
import java.io.*;
import java.util.*;
public class Sort
{
public static void main(String[] args)
{
String sUnsorted = null;
String[] saSplit = null;
int iCurrentWordCount = 1;
String currentword = null;
String pastword = "";
// Read the text file into a string
sUnsorted = readIn("input1.txt");
// Parse the String by white space into String array of single words
saSplit = sUnsorted.split("\\s+");
// Sort the String array in descending order
java.util.Arrays.sort(saSplit, Collections.reverseOrder());
// Count the occurences of each word in the String array
for (int i = 0; i < saSplit.length; i++)
{
currentword = saSplit[i];
// If this word was seen before, increase the count & print the
// word to stdout
if (currentword.equals(pastword))
{
iCurrentWordCount ++;
System.out.println(currentword);
}
// Output the count of the LAST word to stdout,
// Reset our counter
else if (!currentword.equals(pastword))
{
if (!pastword.equals(""))
{
System.out.println("Word Count for " + pastword + ": " + iCurrentWordCount);
}
System.out.println(currentword);
iCurrentWordCount = 1;
}
pastword = currentword;
}// end for loop
// Print out the count for the last word processed
System.out.println("Word Count for " + currentword + ": " + iCurrentWordCount);
}// end funciton main()
// Read The Input File Into A String
public static String readIn(String infile)
{
String result = " ";
try
{
FileInputStream file = new FileInputStream (infile);
DataInputStream in = new DataInputStream (file);
byte[] b = new byte[ in.available() ];
in.readFully (b);
in.close();
result = new String (b, 0, b.length, "US-ASCII");
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}// end funciton readIn()
}// end class Sort()
/////////////////////////////////////////////////
// Updated Copy 1, Based On The Useful Comments
//////////////////////////////////////////////////
import java.io.*;
import java.util.*;
public class Sort2
{
public static void main(String[] args) throws Exception
{
// Scanner will tokenize on white space, like we need
Scanner scanner = new Scanner(new FileInputStream("input1.txt"));
ArrayList <String> wordlist = new ArrayList<String>();
String currentword = null;
String pastword = null;
int iCurrentWordCount = 1;
while (scanner.hasNext())
wordlist.add(scanner.next());
// Sort in descending natural order
Collections.sort(wordlist);
Collections.reverse(wordlist);
for (String temp : wordlist)
{
currentword = temp;
// If this word was seen before, increase the count & print the
// word to stdout
if (currentword.equals(pastword))
{
iCurrentWordCount ++;
System.out.println(currentword);
}
// Output the count of the LAST word to stdout,
// Reset our counter
else //if (!currentword.equals(pastword))
{
if (pastword != null)
System.out.println("Count for " + pastword + ": " +
CurrentWordCount);
System.out.println(currentword);
iCurrentWordCount = 1;
}
pastword = currentword;
}// end for loop
System.out.println("Count for " + currentword + ": " + iCurrentWordCount);
}// end funciton main()
}// end class Sort2
突出的第一件事就是您的C++背景。如果您嘗試使解決方案面向對象,即使問題沒有具體要求,您也可以從練習中獲得更多。讓它更加面向對象將讓你思考如何將功能組合到邏輯類中,並隱藏更方便的方法調用後面的實現細節。也就是說,有時間閱讀更多的代碼並更直接地解決您的問題...... – 2011-06-07 16:46:36
您的命名約定對於現代Java而言是非常殘酷的。對任何版本的Java來說,甚至不一致的匈牙利符號都不是慣用的!直接使用'Array'也被忽略了,還有'List'和'Set'類更具慣用性。 – 2011-06-07 16:47:38
Jarrod。我瞭解有關匈牙利符號的評論。爲什麼List或Set類比在這種情況下使用Array更好? – Steve 2011-06-07 17:10:43