Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更高效还是更现代?阅读及;使用Java对文本文件进行排序_Java_File_Sorting_Text_Collections - Fatal编程技术网

更高效还是更现代?阅读及;使用Java对文本文件进行排序

更高效还是更现代?阅读及;使用Java对文本文件进行排序,java,file,sorting,text,collections,Java,File,Sorting,Text,Collections,我一直在努力提升我的Java技能,以便更多地使用Java5和Java6。我一直在做一些编程练习。我被要求从一个文本文件中读入一个段落,输出一个排序(降序)的单词列表,并输出每个单词的计数 我的代码如下 我的问题是: 我的文件输入例程是最尊重JVM资源的吗 在读取文件内容并将内容放入一个可以生成单词排序列表的集合中时,是否可以减少步骤 我是否以最有效的方式使用集合类和接口 谢谢你的意见。我只是想找点乐子,提高我的编程技能 import java.io.*; import java.util.*;

我一直在努力提升我的Java技能,以便更多地使用Java5和Java6。我一直在做一些编程练习。我被要求从一个文本文件中读入一个段落,输出一个排序(降序)的单词列表,并输出每个单词的计数

我的代码如下

我的问题是:

  • 我的文件输入例程是最尊重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
    
    import java.io.*;
    导入java.util.*;
    公共类排序
    {
    公共静态void main(字符串[]args)
    {
    字符串sUnsorted=null;
    字符串[]saSplit=null;
    int iCurrentWordCount=1;
    字符串currentword=null;
    字符串password=“”;
    //将文本文件读入字符串
    sUnsorted=readIn(“input1.txt”);
    //通过空格将字符串解析为单个单词的字符串数组
    saSplit=sUnsorted.split(\\s+);
    //按降序对字符串数组排序
    sort(saSplit,Collections.reverseOrder());
    //计算字符串数组中每个单词的出现次数
    for(int i=0;i
  • 在Java中,有更多的惯用方法读取文件中的所有单词。 是一种更好的从输入中读取单词的方法

  • 几乎在所有情况下都使用
    List
    而不是
    Array
    。从技术上讲,数组不是
    集合API的一部分,也不像
    列表
    集合
    映射
    那样容易替换实现

  • 您应该使用
    映射
    进行字数计算,而不是在
    数组
    中反复移动。与
    Integer
    不同,它是可变的,因此您可以在一个恰好是线程安全的操作中执行
    incrementAndGet()
    。一个
    SortedMap
    实现将为您提供单词的顺序及其计数

  • 并在使用它们之前声明它们,而不是在其预期范围将丢失的顶部

  • 你几乎应该永远都是你
    void increment(Map<String, Integer> wordCountMap, String word) {
      Integer count = wordCountMap.get(word);
      wordCountMap.put(word, count == null ? 1 : ++count);
    }
    
    for (int i = 0; i < saSplit.length; i++ ){
        currentword = saSplit[i];
        [...]
    }
    
    if ( currentword.equals(pastword) ){
        [...]
    } else if (!currentword.equals(pastword)) {
        [...]
    }
    
    if ( !pastword.equals("") )
    
    if (!pastword.length == 0)