簡體   English   中英

如何按字母順序對我的輸出進行排序

[英]how to sort my output alphabetically java

我正在嘗試讀取2個txt文件,其中一個混雜,一個字典文件,以匹配具有相同字母的文件。 但是,當單詞有3個組合時,就不會按字母順序出現。

我該怎么做才能解決此問題?

import java.io.*;
import java.util.*;

public class Project6
{

    public static void main( String args[] ) throws Exception
    {
        if (args.length<2)
        {System.out.println( "Must pass name of 2 input files on cmd line. (dictionary.txt, jumbles.txt)");
            System.exit(0);
        }
        long startTime = System.currentTimeMillis();  // like clicking START on your stopwatch

        int maxDictWords = 180000;   //has fixed length data structure... was not that familiar with array list and this is faster.

        int maxJumbleWords = 100;         

        BufferedReader dictFile = new BufferedReader( new FileReader(args[0]) ); 

        BufferedReader jumblesFile = new BufferedReader( new FileReader(args[1]) );


        String[] dictionary = new String[maxDictWords]; // save dictionary txt into an array

        String[] jumbles = new String[maxJumbleWords];// save jumblestxt into an array

        String [] sortDict = new String [maxDictWords]; //into a sort dictionary

        String [] sortJumbles = new String [maxJumbleWords]; // into sort jumbles


        int dWordCnt=0, jWordCnt=0;

        Arrays.sort(dictionary, 0, dWordCnt);
        // For file reading 


        while ( dictFile.ready() )   
        {
            String dictionaryWord = dictFile.readLine();  // grab the whole line because the whole line is just one word

            dictionary[dWordCnt] = dictionaryWord;

            String scrambleWord =  toCanonical(dictionaryWord);

            sortDict [dWordCnt] = scrambleWord; 

            dWordCnt++;
        }

        dictFile.close();

         while ( jumblesFile.ready() )   // same as above
        {
            String jumblesWord = jumblesFile.readLine();  
            jumbles[jWordCnt] = jumblesWord;

            String scrambleWord = toCanonical(jumblesWord);

            sortJumbles [jWordCnt] = scrambleWord;

            jWordCnt++;
        }

        jumblesFile.close();

        String [] result = new String [maxJumbleWords];
        for(int k= 0; k < jWordCnt; k++){

             { result[k]= jumbles[k] + " " ;
            for (int j = 0; j < dWordCnt; j++)
            {
                if (sortJumbles[k].equals(sortDict[j]))
                {

                result[k]= result[k] + dictionary[j] + " ";
                } 
          }
         }
         }

        Arrays.sort(result,0, jWordCnt);
        for(int k = 0; k < jWordCnt; k++){

            System.out.println(result[k]);
        }
         long endTime = System.currentTimeMillis();  // like clicking STOP on your stopwatch
        long ms = endTime-startTime;
        System.out.println("Elapsed time in seconds: " + ms/1000.0 + "\n"); // 1 ms is a 1,000th of a second

    } // END MAIN

    // Methods


    // to sort the letter into a canonical form 
    private static String toCanonical(String word )
    {
       char[]Arr = word.toCharArray();
        Arrays.sort(Arr);
        String sorted = new String(Arr);
        return sorted;          
    }


} // END Project 6

如果可以選擇,為什么不在Collection中使用它通過sorted()方法sorted()著名的“詞典”排序呢?

例如(在IDEONE上嘗試過):

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> g = new ArrayList<String>();
        g.add("glove");
        g.add("golve");
        g.add("gvloe");
        g.add("gevlo");

        // Before sorting (Natural order)
        for(String s: g){

            System.out.println(s);
        }

        System.out.println("\n----------");
        Collections.sort(g);

        // After sorting (Natural order)
        for(String s: g){

            System.out.println(s);
        }
    }

    System.out.println("----");
    // Convert it to array
    String[] p = g.toArray(new String[g.size()]);

    // Check if the array has the correct sort order?

    for (String s: p){
        System.out.println(s);
    }
}

在行動

這對您有用嗎? 注意上面的一些代碼將是多余的,因為您可以進行排序,然后將其轉換為數組(無論如何它將具有正確的排序順序)。 我只是展示了它,以幫助您了解您可能更喜歡的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM