我希望遍历一个单词并打印出它的所有不同变体。我已经编写了代码,但由于某种原因,我不断获得StringIndexOutOfBoundsException
.
public class Test {
public static void main(String[] args) {
String word = "telecommunications"; // loop thru this word
for (int i = 0; i < word.length(); i++) {
for (int j = 0; j < word.length(); j++) {
System.out.println(word.substring(i, j + 1));
//This will print out all the different variations of the word
}
}
}
}
有人可以告诉我为什么会收到此错误吗?