我有一段这样的代码:
while (scanFile.hasNextLine())
{
String currentLine = scanFile.nextLine();
if (currentLine.isEmpty())
{
System.out.println();
continue;
}
String[] allWordsInCurrentLine = currentLine.split(" ");
然后,我想使用三种不同的方法来操作正在扫描的文件。
第一个方法单独扫描代码的每一行,并逐行打印出某种形式的输出(pigLatin)(因此“while”循环的每次迭代都会给出一行输出。
第二种方法首先扫描所有文本并将各种信息存储在不同的数组和变量中。然后用户可以搜索该信息(因此在给出任何输出之前必须满足整个“while”循环)。
然后我有第三种方法,它的功能与第一种方法类似。
我正在尝试找到重用这部分代码的最有效方法。最初,我尝试只在上面代码的底行下方进行方法调用,并在其顶部有一个 for 循环,因此如果 a==0,则调用第一个方法,然后第二次通过 while 循环,a 将递增并将调用第二个方法,依此类推,如下所示:
for(String currentWordInCurrentLine : allWordsInCurrentLine)
{
if (i==0)
pigLatin(currentWordInCurrentLine); // Part 1
if (i==1)
searchForWord(currentWordInCurrentLine, currentLine); // Part 2
if (i==2)
brailleTranslator();
}
这有一些问题,尤其是它看起来很糟糕。有人建议我尝试界面,但它们有点超出我的水平(我刚开始类(class)几周)。大家还有其他建议吗?
编辑:pigLatin 方法调用使用上述(尽管看起来很糟糕)方式可以正常工作。但是,我无法像这样调用下一个方法,因为它需要运行整个“while”循环才能运行。
最佳答案
我会将您想要的功能与三个不同的进程包装在它们自己的自定义可调用类中,然后将它们传递给上面的代码。在这种情况下,共享代码将如下所示:
public class FileHandlerManager {
public static void processFile(File file, List<FileHandler> handlers) {
// ... Setup your scanFile from your file... leaving this out.
while (scanFile.hasNextLine()) {
String currentLine = scanFile.nextLine();
String[] allWordsInCurrentLine = currentLine.split(" ");
for (FileHandler handler : handlers) {
handler.handle(allWordsInCurrentLine);
}
}
}
}
然后您可以在 FileHandler
之间共享 while 循环的功能。什么是FileHandler
?我会非常简单地定义它,如下所示:
public interface FileHandler {
public void handle(String[] allWordsInCurrentLine);
}
您的第一个可能如下所示:
public class PigLatinFileHandler implements FileHandler {
public void handle(String[] allWordsInCurrentLine) {
// Do your piglatin code.
}
}
最后,要运行它,您需要执行以下操作
public static void main(String[] args) {
IndexingFileHandler indexHandler = new IndexingFileHandler();
List<FileHandler> handlers = new LinkedList<FileHandler>();
handlers.add(new PigLatinFileHandler());
handlers.add(indexHandler);
handlers.add(new SimilarToPigLatinFileHandler());
FileHandlerManager.processFile(file, handlers);
// Note now you have a reference to indexHandler; so if you wanted to save
// any state while processing the words you can, and use it here.
}
编辑
我只是想指出,有一种“更简单”、更少面向对象的方法;您可以有一个方法获取一个文件并返回一个 String[][],它是文件行的二维数组,按这些行中的单词排列。然后您可以将该数组传递给您的处理程序。
然而,这种方法的缺点是它会破坏大文件(您必须立即将整个文件加载到内存中),并且在读取整个文件之前您不会得到任何输出。我的答案的一个(也许不明显)优点是您可以“流式传输”文件;您使用所有处理程序处理一行,然后丢弃该行,从而允许 java 重用该内存。这段代码可以让你处理一个 10 GB 的文件,同时只使用等于该文件中最长行长度的 java 内存(加上 IndexHandler 正在保存的任何状态)。
关于java - 重用一小部分代码的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13573830/