ファイルから入力を読み取り、各単語内の文字をソートする Java プログラムを作成します。それが完了したら、結果のすべての単語を昇順に並べ替え、最後にファイル内の数値の合計を続けます。
- データの処理中に特殊文字とストップ ワードを削除する
- コードの実行にかかった時間を測定する
ファイルの内容は次のとおりです。Sachin Tendulkar は 18111 回の ODI 実行と 14692 回のテスト実行を記録しました。
出力:achins adeklnrtu adn cdeors dio estt nrsu nrsu 32803
所要時間: 3 ミリ秒
私のコードの実行には 15 ミリ秒かかります.....
この問題を解決する手っ取り早い方法を教えてください........
コード:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Sorting {
public static void main(String[] ags)throws Exception
{
long st=System.currentTimeMillis();
int v=0;
List ls=new ArrayList();
//To read data from file
BufferedReader in=new BufferedReader(
new FileReader("D:\\Bhive\\File.txt"));
String read=in.readLine().toLowerCase();
//Spliting the string based on spaces
String[] sp=read.replaceAll("\\.","").split(" ");
for(int i=0;i<sp.length;i++)
{
//Check for the array if it matches number
if(sp[i].matches("(\\d+)"))
//Adding the numbers
v+=Integer.parseInt(sp[i]);
else
{
//sorting the characters
char[] c=sp[i].toCharArray();
Arrays.sort(c);
String r=new String(c);
//Adding the resulting word into list
ls.add(r);
}
}
//Sorting the resulting words in ascending order
Collections.sort(ls);
//Appending the number in the end of the list
ls.add(v);
//Displaying the string using Iteartor
Iterator it=ls.iterator();
while(it.hasNext())
System.out.print(it.next()+" ");
long time=System.currentTimeMillis()-st;
System.out.println("\n Time Taken:"+time);
}
}