簡體   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