gpt4 book ai didi

java - 线程 "main"中的异常 java.lang.StringIndexOutOfBoundsException : String index out of range: -1

转载 作者:行者123 更新时间:2023-12-04 06:35:49 25 4
gpt4 key购买 nike

 public static String s1 = "team A won the match historically to clinch series which made surprise around the world ";
public static String s2 = "He did his part";

t1=s1.length();
t2=s2.length();
t3=Math.abs(t1-t2);

for(i=0;i<t3;i++)
{
Sub_string.add(s1.substring(i,t2++));
}

我该如何解决这个问题?

最佳答案

试过了。似乎没有抛出异常

我猜您将列表定义为最大尺寸?如果你这样定义它就会起作用。

public class TestCode {
public static String s1 = "team A won the match historically to clinch series which made surprise around the world ";
public static String s2 = "He did his part";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> Sub_string = new ArrayList<String>();

int t1=s1.length();
int t2=s2.length();
int t3=Math.abs(t1-t2);


try {
for(int i=0;i<t3;i++)
{
Sub_string.add(s1.substring(i,t2++));
}
} catch (Exception e) {
System.err.println();

}

}

}

关于java - 线程 "main"中的异常 java.lang.StringIndexOutOfBoundsException : String index out of range: -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4895159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com