ここにその方法があります。私はここでベストプラクティスに違反しているのか、それとも言語に関しては何か間違っていることを知りたいのです。このメソッドを最適化して文字列を分割する方法を教えてください。
private List<String> breakStringInChunks(String text, int chunkSize) {
List<String> chunks = new ArrayList<String>();
String temporary = "";
int numberOfChunks = text.length()/chunkSize;
int beginIndex = 0;
int endIndex = 0;
// Add one iteration if numberOfChunks*chunkSize is less than the length of text.
if ((numberOfChunks * chunkSize) < text.length()) {
numberOfChunks++;
}
// Cut strings and add in the list.
for (int i = 0; i < numberOfChunks; i++) {
endIndex+=chunkSize;
if ((i + 1) == numberOfChunks) {
temporary = text.substring(beginIndex);
}
else {
temporary = text.substring(beginIndex, endIndex);
}
beginIndex=endIndex;
chunks.add(temporary);
}
return chunks;
}
なぜ、文字列をチャンク化するのですか? –