gpt4 book ai didi

java.lang.StringIndexOutOfBoundsException 错误 我做错了什么

转载 作者:行者123 更新时间:2023-12-01 13:17:15 24 4
gpt4 key购买 nike

public int countMatchesWithRightShift(DNAStrand other, int shift){

for (j = 0; j < (data.length()); ++j){
other.data.charAt(j = other.data.charAt(j)+shift);
}
for (i = 0; i <other.length()-data.length(); ++i){

if (other.data.charAt(i) == 'T' && data.charAt(i) == 'A'){
rightShift++;
}
else if (other.data.charAt(i) == 'A' && data.charAt(i) == 'T'){
rightShift++;
}
else if (other.data.charAt(i) == 'C' && data.charAt(i) =='G'){
rightShift++;
}
else if (other.data.charAt(i) == 'G' && data.charAt(i) =='C'){
rightShift++;
}
}

return rightShift;
}

这是为了比较两条不同的 DNA 链,其中一条 DNA 链移动了 int 量。当我运行提供给我们的规范检查器时,测试不断给出越界错误。我不确定我做错了什么

最佳答案

这条线很狡猾

other.data.charAt(j = other.data.charAt(j)+shift);

您的意思是通过执行以下操作来设置字符串中字符的值

other.data.charAt(j) = other.data.charAt(j)+shift;

这也是不可能的,因为字符串是不可变的

尝试

String other.data = other.data.substring(0,j) + other.data.charAt(j)+shift 
+other.data.substring(j + 1);

//索引关闭的警告测试(这里没有 IDE)

关于java.lang.StringIndexOutOfBoundsException 错误 我做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367331/

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