के साथ एक टेक्स्ट फ़ाइल में पढ़ना और सॉर्ट करना मैं जावा 5 & जावा 6 का उपयोग करने के लिए अपने जावा कौशल को अपग्रेड करने का प्रयास कर रहा हूं। मैं कुछ प्रोग्रामिंग अभ्यासों के साथ खेल रहा हूं। मुझे एक टेक्स्ट फ़ाइल से अनुच्छेद में पढ़ने और शब्दों की एक क्रमबद्ध (अवरोही) सूची आउटपुट करने और प्रत्येक शब्द की गिनती आउटपुट करने के लिए कहा गया था।अधिक कुशल या अधिक आधुनिक? जावा
मेरा कोड नीचे है।
मेरे प्रश्न हैं:
मेरी फ़ाइल इनपुट दिनचर्या 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
आपकी पहली सी ++ पृष्ठभूमि है जो पहली चीज है। यदि आप अपने समाधान ऑब्जेक्ट उन्मुख बनाने का प्रयास करते हैं, तो भी आप व्यायाम से अधिक लाभ प्राप्त कर सकते हैं, भले ही प्रश्न विशेष रूप से इसके लिए नहीं पूछें। इसे अधिक ऑब्जेक्ट-ओरिएंटेड बनाने से आप लॉजिकल क्लास में कार्यक्षमता को समूहबद्ध करने और अधिक सुविधाजनक विधि कॉल के पीछे कार्यान्वयन विवरण छुपाने के बारे में सोचेंगे। उस ने कहा, आपके कोड को और अधिक पढ़ने और अपने प्रश्न को अधिक सीधे संबोधित करने का समय ... –
आपके नामकरण सम्मेलन आधुनिक जावा के लिए अत्याचारी हैं। हंगेरियन नोटेशन जो कि संगत नहीं है, किसी भी संस्करण के जावा के लिए मूर्ख नहीं है! सीधे 'ऐरे' का उपयोग करके भी फेंक दिया जाता है, वहां 'सूची' और' सेट 'कक्षाएं होती हैं जो अधिक मूर्खतापूर्ण होती हैं। –
जारोड। मैं हंगेरियन नोटेशन के बारे में टिप्पणी को समझता हूं। इस स्थिति में एक ऐरे का उपयोग करने से सूची या सेट कक्षाएं बेहतर क्यों हैं? – Steve