获取错误StringIndexOutOfBoundsException:字符串索引超出范围

时间:2012-10-18 21:18:36

标签: java

我正在尝试构建一个矩阵,其行和列具有诸如“aaa”之类的值以用于对齐目的。但是当我运行它时,我得到一个错误。下面是我的代码

public class compute_matrix {
  static String seq1="aaa";
  static String seq2="aaa";
  static int[][] matrix;
  static int max_row;
  static int max_col;
  private static int match_reward=1;
  private static int mismatch_penalty= -1;
  private static int gap_cost= -1;
  private static boolean case_sensitive;

  private static boolean isCaseSensitive() {
    return case_sensitive;
  }

  private static int max(int ins, int sub, int del, int i) {
    if (ins > sub) {
        if (ins > del) {
            return ins > i? ins : i;
        } else {
            return del > i ?del : i;
        }
    } else if (sub > del) {
        return sub> i ? sub : i;
    } else {
        return del > i ? del : i;
    }
}

 protected char sequence[];

   public static void main(String args[]){
    int r, c, rows, cols, ins, sub, del, max_score;

    rows = seq1.length()+1;
    cols = seq2.length()+1;

    matrix = new int [rows][cols];

    // initiate first row
    for (c = 0; c < cols; c++)
        matrix[0][c] = 0;

    // keep track of the maximum score
    max_row = max_col = max_score = 0;

    // calculates the similarity matrix (row-wise)
    for (r = 1; r < rows; r++)
    {
        // initiate first column
        matrix[r][0] = 0;

        for (c = 1; c < cols; c++)
        {
                        sub = matrix[r-1][c-1] + scoreSubstitution(seq1.charAt(r),seq2.charAt(c));
            ins = matrix[r][c-1] + scoreInsertion(seq2.charAt(c));

            del = matrix[r-1][c] + scoreDeletion(seq1.charAt(r));

            // choose the greatest
            matrix[r][c] = max (ins, sub, del, 0);

            if (matrix[r][c] > max_score)
            {
                // keep track of the maximum score
                max_score = matrix[r][c];
                max_row = r; max_col = c;
            }
        }
    }
     }

private static int scoreSubstitution(char a, char b) {
   if (isCaseSensitive())
        if (a == b)
            return match_reward;
        else
            return mismatch_penalty;
    else
        if (Character.toLowerCase(a) == Character.toLowerCase(b))
            return match_reward;
        else
            return mismatch_penalty;
}

private static int scoreInsertion(char a) {
 return gap_cost;
}

private static int scoreDeletion(char a) {
    return gap_cost;
}
public char charAt (int pos)
{
    // convert from one-based to zero-based index
    return sequence[pos-1];
}

  }

我的错误是显示此

              Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(String.java:695)
    at compute_matrix.main(compute_matrix.java:67)

Java结果:1

3 个答案:

答案 0 :(得分:4)

   rows = seq1.length()+1;
    cols = seq2.length()+1;

    matrix = new int [rows][cols];

然后再说:

   for (c = 1; c < cols; c++)
    {
   //when c == cols-1, it is also `seq2.length()`
   //the access to seq2.charAt(c) will cause this exception then.
                    sub = matrix[r-1][c-1] + scoreSubstitution(seq1.charAt(r),seq2.charAt(c));
        ins = matrix[r][c-1] + scoreInsertion(seq2.charAt(c));

        del = matrix[r-1][c] + scoreDeletion(seq1.charAt(r));

在上面的循环中,当c == cols-1也是seq2.length()时,对seq2.charAt(c)的访问会导致此异常。

您将行数和列数初始化为length() + 1,而稍后从0迭代到长度(包括),而字符串仅包含length()个字符 - 从0到n独占。

如果您是过去的C程序员 - 我假设您期望在字符串末尾有一个\0终止符。在java中你没有那些 - 因为String是一个对象 - 你可以保持一个字段来指示它的确切长度。这意味着字符串中的最后一个字符实际上是那里的最后一个字符。

答案 1 :(得分:0)

在代码的第60行 sub = matrix[r-1][c-1] + scoreSubstitution(seq1.charAt(r),seq2.charAt(c)); r的最大值是4,所以当你查找seq.charAt(3)时,没有任何东西显示索引超出范围

答案 2 :(得分:0)

我将你的代码重构为更规范的java。

我改变的事情:

  • 该类现在称为SimilarityMatrix,一个更合适的自我记录名称
  • 变量声明现在发生在使用它们而不是在主
  • 的顶部
  • 现在,该工作是在类的实例中完成的,而不是主方法
  • 我使用内置的Math.max(int, int)而不是滚动自己的
  • 我删除了很多不必要的嵌套if语句。 Java的短路评估有助于此
  • 由于您的计算循环中经常使用rc以及r+1c+1,因此我会同时跟踪
  • 我删除了很多关于静态的依赖(使很多事情实例变量)
  • 剩下的静态现在都是最终的(我使它们成为常量)
  • 使用了更多java-y变量名(java人真的喜欢他们的驼峰案例)

public class SimilarityMatrix
{
    public static final int matchReward = 1;
    public static final int mismatchPenalty = -1;
    public static final int gapCost = -1;

    private int[][] matrix;
    private int maxRow = 0;
    private int maxCol = 0;
    private boolean caseSensitive = false;

    SimilarityMatrix(String s1, String s2, boolean dontIgnoreCase)
    {
        this(s1, s2);
        caseSensitive = dontIgnoreCase;
    }

    SimilarityMatrix(String s1, String s2)
    {
        int rows = s1.length() + 1;
        int cols = s2.length() + 1;
        matrix = new int[rows][cols];

        int max_score = 0;

        for (int x = 0; x < cols; x++)
        {
            matrix[0][x] = 0;
            matrix[x][0] = 0;
        }

        for (int r = 0, rp1 = 1; rp1 < rows; ++r, ++rp1)
        {
            for (int c = 0, cp1 = 1; cp1 < rows; ++c, ++cp1)
            {
                int sub = matrix[r][c] + scoreSubstitution(s1.charAt(r), s2.charAt(c));
                int ins = matrix[rp1][c] + scoreInsertion(s2.charAt(c));

                int del = matrix[r][cp1] + scoreDeletion(s1.charAt(r));

                // choose the greatest
                matrix[rp1][cp1] = Math.max(Math.max(ins, sub), Math.max(del, 0));

                if (matrix[rp1][cp1] > max_score)
                {
                    // keep track of the maximum score
                    max_score = matrix[rp1][cp1];
                    maxRow = rp1;
                    maxCol = cp1;
                }
            }
        }
    }

    public static void main(String args[])
    {
        SimilarityMatrix me = new SimilarityMatrix("aaa", "aaa");
        System.out.println(me.getMaxRow() + " " + me.getMaxCol());
    }

    private int scoreSubstitution(char a, char b)
    {
        if ((a == b && caseSensitive) || Character.toLowerCase(a) != Character.toLowerCase(b))
            return matchReward;
        else
            return mismatchPenalty;
    }

    public int getMaxRow()
    {
        return maxRow;
    }

    public int getMaxCol()
    {
        return maxCol;
    }

    private int scoreInsertion(char a)
    {
        return gapCost;
    }

    private int scoreDeletion(char a)
    {
        return gapCost;
    }
}