您只需要调用fullJustify() 方法,其中需要在单词列表中传递您想要输出的每行的最大宽度。
public List<String> fullJustify(String[] words, int maxWidth) {
int n = words.length;
List<String> justifiedText = new ArrayList<>();
int currLineIndex = 0;
int nextLineIndex = getNextLineIndex(currLineIndex, maxWidth, words);
while (currLineIndex < n) {
StringBuilder line = new StringBuilder();
for (int i = currLineIndex; i < nextLineIndex; i++) {
line.append(words[i] + " ");
}
currLineIndex = nextLineIndex;
nextLineIndex = getNextLineIndex(currLineIndex, maxWidth, words);
justifiedText.add(line.toString());
}
for (int i = 0; i < justifiedText.size() - 1; i++) {
String fullJustifiedLine = getFullJustifiedString(justifiedText.get(i).trim(), maxWidth);
justifiedText.remove(i);
justifiedText.add(i, fullJustifiedLine);
}
String leftJustifiedLine = getLeftJustifiedLine(justifiedText.get(justifiedText.size() - 1).trim(), maxWidth);
justifiedText.remove(justifiedText.size() - 1);
justifiedText.add(leftJustifiedLine);
return justifiedText;
}
public static int getNextLineIndex(int currLineIndex, int maxWidth, String[] words) {
int n = words.length;
int width = 0;
while (currLineIndex < n && width < maxWidth) {
width += words[currLineIndex++].length() + 1;
}
if (width > maxWidth + 1)
currLineIndex--;
return currLineIndex;
}
public String getFullJustifiedString(String line, int maxWidth) {
StringBuilder justifiedLine = new StringBuilder();
String[] words = line.split(" ");
int occupiedCharLength = 0;
for (String word : words) {
occupiedCharLength += word.length();
}
int remainingSpace = maxWidth - occupiedCharLength;
int spaceForEachWordSeparation = words.length > 1 ? remainingSpace / (words.length - 1) : remainingSpace;
int extraSpace = remainingSpace - spaceForEachWordSeparation * (words.length - 1);
for (int j = 0; j < words.length - 1; j++) {
justifiedLine.append(words[j]);
for (int i = 0; i < spaceForEachWordSeparation; i++)
justifiedLine.append(" ");
if (extraSpace > 0) {
justifiedLine.append(" ");
extraSpace--;
}
}
justifiedLine.append(words[words.length - 1]);
for (int i = 0; i < extraSpace; i++)
justifiedLine.append(" ");
return justifiedLine.toString();
}
public String getLeftJustifiedLine(String line, int maxWidth) {
int lineWidth = line.length();
StringBuilder justifiedLine = new StringBuilder(line);
for (int i = 0; i < maxWidth - lineWidth; i++)
justifiedLine.append(" ");
return justifiedLine.toString();
}
以下是 maxWidth 为 80 个字符的示例转换:
以下段落包含 115 个单词,将转换后的文本写入外部文件需要 55 毫秒。
我已经针对大约 70k+ 个单词的段落测试了这段代码,并且大约需要 400 毫秒 将转换后的文本写入文件。
输入
这些功能往往使法律写作正式化。这个手续可以
采取长句、复杂结构、古旧和
超正式的词汇,并专注于内容以排除
读者需求。法律文书中的某些形式是必要的,并且
可取的,考虑到一些法律文件的重要性和
某些法律文件的情节严重性
用过的。然而,并非法律文书中的所有形式都是合理的。到
形式化产生不透明和不精确的程度,它是
不可取。形式妨碍读者阅读的程度
理解,它是不太可取的。特别是,当法律内容
必须传达给非律师,形式应让位于明确
交流。
输出
These features tend to make legal writing formal. This formality can take the
form of long sentences, complex constructions, archaic and hyper-formal
vocabulary, and a focus on content to the exclusion of reader needs. Some of
this formality in legal writing is necessary and desirable, given the importance
of some legal documents and the seriousness of the circumstances in which some
legal documents are used. Yet not all formality in legal writing is justified.
To the extent that formality produces opacity and imprecision, it is
undesirable. To the extent that formality hinders reader comprehension, it is
less desirable. In particular, when legal content must be conveyed to
nonlawyers, formality should give way to clear communication.