当前位置: 技术问答>java相关
一个程序,问题比较弱,高手有空看看
来源: 互联网 发布时间:2015-02-05
本文导语: reader类的作用?printwriter类的作用?reverse和sort中的两个线程是否一起执行,换句话说,是不是reverse操作一行文本,就立刻传送给sort? import java.io.*; public class RhymingWords { public static void main(String[] args) throw...
reader类的作用?printwriter类的作用?reverse和sort中的两个线程是否一起执行,换句话说,是不是reverse操作一行文本,就立刻传送给sort?
import java.io.*;
public class RhymingWords {
public static void main(String[] args) throws IOException {
FileReader words = new FileReader("words.txt");
// do the reversing and sorting
Reader rhymedWords = reverse(sort(reverse(words)));
// write new list to standard out
BufferedReader in = new BufferedReader(rhymedWords);
String input;
while ((input = in.readLine()) != null)
System.out.println(input);
in.close();
}
public static Reader reverse(Reader source) throws IOException {
BufferedReader in = new BufferedReader(source);
PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader(pipeOut);
PrintWriter out = new PrintWriter(pipeOut);
new ReverseThread(out, in).start();
return pipeIn;
}
public static Reader sort(Reader source) throws IOException {
BufferedReader in = new BufferedReader(source);
PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader(pipeOut);
PrintWriter out = new PrintWriter(pipeOut);
new SortThread(out, in).start();
return pipeIn;
}
}
import java.io.*;
public class SortThread extends Thread {
private PrintWriter out = null;
private BufferedReader in = null;
public SortThread(PrintWriter out, BufferedReader in) {
this.out = out;
this.in = in;
}
public void run() {
int MAXWORDS = 50;
if (out != null && in != null) {
try {
String[] listOfWords = new String[MAXWORDS];
int numwords = 0;
while ((listOfWords[numwords] = in.readLine()) != null)
numwords++;
quicksort(listOfWords, 0, numwords-1);
for (int i = 0; i = hi)
return;
String mid = a[(lo + hi) / 2];
while (lo
import java.io.*;
public class RhymingWords {
public static void main(String[] args) throws IOException {
FileReader words = new FileReader("words.txt");
// do the reversing and sorting
Reader rhymedWords = reverse(sort(reverse(words)));
// write new list to standard out
BufferedReader in = new BufferedReader(rhymedWords);
String input;
while ((input = in.readLine()) != null)
System.out.println(input);
in.close();
}
public static Reader reverse(Reader source) throws IOException {
BufferedReader in = new BufferedReader(source);
PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader(pipeOut);
PrintWriter out = new PrintWriter(pipeOut);
new ReverseThread(out, in).start();
return pipeIn;
}
public static Reader sort(Reader source) throws IOException {
BufferedReader in = new BufferedReader(source);
PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader(pipeOut);
PrintWriter out = new PrintWriter(pipeOut);
new SortThread(out, in).start();
return pipeIn;
}
}
import java.io.*;
public class SortThread extends Thread {
private PrintWriter out = null;
private BufferedReader in = null;
public SortThread(PrintWriter out, BufferedReader in) {
this.out = out;
this.in = in;
}
public void run() {
int MAXWORDS = 50;
if (out != null && in != null) {
try {
String[] listOfWords = new String[MAXWORDS];
int numwords = 0;
while ((listOfWords[numwords] = in.readLine()) != null)
numwords++;
quicksort(listOfWords, 0, numwords-1);
for (int i = 0; i = hi)
return;
String mid = a[(lo + hi) / 2];
while (lo