-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 回答