-4

だから私が取り組んでいるコードはこのようになります

import java.util.Scanner;
public class ReadStrings{
public static String main(String[] args) {
Scanner in = new Scanner(new File("input3.txt"));
String [] array = new String[100];
int nextSpot = 0;

while( in.hasNext()){
  array[nextSpot++] = in.next();
}
//use the sort function
selectionSort(array, nextSpot);
//print the results

}



public static void selectionSort(String [] array, int nextSpot){
  String tmp;
  for (int i = 0; i < nextSpot; i++) {
    for (int j = i + 1; j < nextSpot; j++) {
      if( array[i].equals(array[j])){
        tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
      }
    }
  }
}
}

テキストファイルが存在すると仮定すると、私のコードに何か問題がありますか?結果の配列を印刷する方法もわかりません

4

1 に答える 1

0

あなたの質問は明確ではありません。結果をファイル自体に出力したいと思います。BufferedWriter次のようなクラスを使用できます。

BufferedWriter writer = new BufferedWriter(new FileWriter(new File("input3.txt")));

そのメソッドを使用しwrite()て配列を出力します。

ただし、 selectionSort()を確認することをお勧めします。

于 2013-02-14T23:31:06.780 に答える