-4
java程序,所以我长相这样工作进行排序,并返回数组
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;
}
}
}
}
}
假设文本文件存在的代码有什么错我的代码? 我也不知道如何打印产生的阵列
这是什么问题?代码是否工作?如果不是,你面临什么问题?你试图解决什么问题? – 2013-02-14 22:20:42
我不知道如何在调用selectionSort后打印结果 – user2073750 2013-02-14 22:24:08
您可以遍历数组,并在主方法中打印每个元素。顺便说一句,我不明白你的选择如何工作。您正在使用'equals'方法而不是'compareTo'。 – 2013-02-14 22:25:24