📄 sortwords.java
字号:
import java.io.*;
import javagently.*;
import myutilities.*;
class SortWords {
/* The Sort Words program by J M Bishop Feb 1997
* ====================== Java 1.1 October 1997
* updated June 2000
* Reads in and then sorts up to 100
* different words.
* Illustrates sorting and searching an array.
*/
void selectionSort(String[] a, int n) {
String temp;
int chosen;
for (int leftmost = 0; leftmost < n-1; leftmost++) {
chosen = leftmost;
for (int j = leftmost+1; j < n; j++)
if (a[j].compareTo(a[chosen])<0)
chosen = j;
temp = a[chosen];
a[chosen] = a[leftmost];
a[leftmost] = temp;
}
}
boolean search (String[] a, int n, String x) {
for (int i = 0; i < n; i++)
if (x.compareTo(a[i])==0) return true;
return false;
}
void report(String[] a, int n) {
for (int i = 0; i < n; i++) {
System.out.print(a[i]+"\t");
if (i>0 && i % 7 == 0) System.out.println();
}
}
SortWords() throws IOException {
String group [] = new String[100];
System.out.print("Where are the words? ");
Stream fin = Filer.open("");
int count=0;
try {
while (count < 100) {
String word = fin.readString();
if (! search(group,count,word)) {
group[count] = word;
count ++;
}
}
} catch (EOFException e) {
System.out.println(count + " words read.\n\n");
}
System.out.print("Original\n");
System.out.print("========\n");
report(group,count);
selectionSort(group, count);
System.out.print("\n\nSorted\n");
System.out.print("======\n");
report(group,count);
}
public static void main(String[] args) throws IOException {
new SortWords();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -