我尝试对逻辑进行编码,以打印给定的字符串是否是其他字符串的排列,
发生运行时错误
“ java.lang.ArrayIndexOutOfBoundsException”
我只访问了字符串数组的两个索引。
->给定的字符串由两个空格分隔。
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
运行时错误消息:“ java.lang.ArrayIndexOutOfBoundsException”
请帮助解决此错误。
答案 0 :(得分:0)
代码的问题是,假设您将在数组中获得两项,但并非所有输入都如此。 您应该考虑数组大小:
public static String permute(String str){
String[] st=str.split("\\s+");
for (String item : st) {
// do whatever
}
}
如果您只想处理前两个项目,而不管是否还有其他项目,请执行类似for (int i = 0 ; i < st.length && i < 2 ; i++)
的操作,但始终在访问数组的项目之前先验证其长度