繁体   English   中英

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

[英]java.lang.StringIndexOutOfBOundsException Error what did i do wrong

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链,将一条链移位一个int量。 当我运行提供给我们的specchecker时,测试不断给我出界错误。 我不确定我做错了什么

这条线很狡猾

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

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM