标签:
/** * */ package com.jason.kwic.mainSubroutine; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; /** * 基于主程序/子程序风格的kiwc实现 * @author jasonzhang * */ public class MainSubroutine { private ArrayList<ArrayList<String>> linelist = new ArrayList<ArrayList<String>>(); private ArrayList<String> shiftedLineList = new ArrayList<String>(); public static void main(String[] args) throws IOException { MainSubroutine main = new MainSubroutine(); main.input(); main.shift(); main.alphabetizer(); main.output(); } private void input() throws IOException { File infile = new File("d:\\temp\\mykwic_in.txt"); Scanner sc = new Scanner(infile); String tempLine = null; System.out.println("input is"); while (sc.hasNextLine()) { tempLine = sc.nextLine(); System.out.println(tempLine); this.linelist.add(this.lineSplitWord(tempLine)); } } private void shift() { for (ArrayList<String> wordLine : this.linelist) { this.shiftWordLine(wordLine); } } private void alphabetizer() { //按字母表排序 Collections.sort(this.shiftedLineList, new AlphaabetizerComparator()); } private void output() { System.out.println("output is"); for (String line : this.shiftedLineList) { System.out.println(line); } } private ArrayList<String> lineSplitWord(String line) { ArrayList<String> wordList = new ArrayList<String>(); String word = ""; int i = 0; while(i < line.length()){ if(line.charAt(i) != ‘ ‘){ word += line.charAt(i); } else{ wordList.add(word); word = ""; } i++; } if (word.length() > 0) { wordList.add(word); } return wordList; } private void shiftWordLine(ArrayList<String> wordLine) { StringBuilder tempLine = new StringBuilder(); int wordNums = wordLine.size(); //System.out.println("shifed is"); for (int i=0; i<wordNums; i++) { for (int j=(wordNums - 1 -i); j < wordNums; j++) { tempLine.append(wordLine.get(j)).append(‘ ‘); } for (int k=0; k<(wordNums - 1 -i); k++) { if (k != (wordNums - i - 2)) { tempLine.append(wordLine.get(k)).append(‘ ‘); } else { tempLine.append(wordLine.get(k)); } } //System.out.println(tempLine.toString()); this.shiftedLineList.add(tempLine.toString()); tempLine.delete(0, tempLine.length()); } } private class AlphaabetizerComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1 == null || o2 == null) { throw new NullPointerException(); } int compareValue = 0; char o1FirstCharacter = o1.charAt(0); char o2FirstCharacter = o2.charAt(0); if(this.isLetter(o1FirstCharacter) && this.isLetter(o2FirstCharacter)) { //如果是小写的字母的值,则转成对应的大写的字母的值 o1FirstCharacter = this.toUpperCase(o1FirstCharacter); o2FirstCharacter = this.toUpperCase(o2FirstCharacter); compareValue = o1FirstCharacter - o2FirstCharacter; } else { throw new RuntimeException("必须是字母"); } return compareValue; } private boolean isLetter(char c) { return (c >= 65 && c <= 90) || (c >= 97 && c <= 122); } private char toUpperCase(char c) { if (Character.isLowerCase(c)) { return Character.toUpperCase(c); } return c; } } }
这种风格基本上就是一个类实现所有的事情。
标签:
原文地址:http://my.oschina.net/u/914897/blog/409151