如何按字母顺序排序我的输出java

时间:2015-03-20 23:00:14

标签: java arrays

我试图读取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

1 个答案:

答案 0 :(得分:1)

如果您有选择,为什么不在Collection中使用它来使用sorted()方法进行着名的“Lexicographical”排序?

例如(在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);
    }
}

in action

这对你有用吗?注:上面的一些代码将是多余的,因为您可以只进行排序,然后将其转换为数组(无论如何都将具有正确的排序顺序)。我刚刚展示它以帮助您了解您可能更喜欢的解决方案。