使用HashMaps查找字符串频率



我的代码旨在从包含 DNA lonf 字符串的文件中查找最常见的密码子(即 CTAAATCGATGGCGATGATAAATG...(。 从初始位置开始pos,每三个字符组成一个密码子。我遇到的问题是,每当我运行代码时,它都会告诉我字符串索引超出范围。我知道问题出在一线

str = line.substring(idx, idx + 2);

但不知道如何解决它。另外,我不确定我是否正确计算了频率。我需要增加多次看到的每个键的值。

public static void findgene(String line){
int idx, pos;
int[] freq = new int[100];
String str;
//pos is the position to start at
pos = 0;
idx = pos;
for(int i = 0; i < line.length(); i++){
if(idx >= 0){
//makes every three characters into a codon
str = line.substring(idx, idx + 2);
//checks if the codon was previously seen
if(genes.containsKey(str)){
genes.put(str, freq[i]++);
}
idx = idx + 2;
}
}
}

每次循环迭代时,您都会将idx递增 2。但是,您没有对idx的上限施加任何限制。

因此,substring()函数的参数很快就会超出该行的范围:

str = line.substring(idx, idx + 2);

您需要做的是将条件更改为:

if(idx+2<=line.length()){
//code here
}

最新更新