以上是我的应用程序的图像。我正在构建一个应用程序来对齐两个字符串。将比较两个字符串并填充一个二维数组。二维数组的值将是矩阵的值。所有工作得很好,我唯一可能遇到的问题是,当它打印矩阵的值时,我希望它以矩阵格式打印,如下例所示,而不是输出“0000000011011000002100013”,我希望它显示为这样
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0 1 0 2 1
0 1 0 1 3
打印出来时,它必须查看打印的字符数是否等于我的行大小,然后移动到下一行并继续打印,直到显示所有矩阵值。以下是我的代码片段。提前谢谢
我的计算矩阵按钮的代码
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
match_reward = Integer.parseInt (match_tf.getText());
mismatch_penalty =Integer.parseInt (mismatch_tf.getText());
gap_cost=Integer.parseInt(gap_tf.getText());
build_matrix();
collumn_tf.setText(String.valueOf(max_col));
row_tf.setText(String.valueOf(max_row));
highest_score_tf.setText(String.valueOf(max_score));
StringBuilder sb = new StringBuilder();
for (int i =0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sb.append(String.valueOf(matrix[i][j]));
sb.append(' ');
}
}
matrix_tf.setText(sb.toString());
}
构建我的矩阵的代码
private void build_matrix() {
String seq1 = sequence1_tf.getText();
String seq2 = sequence2_tf.getText();
int r, c, ins, sub, del;
rows = seq1.length();
cols = seq2.length();
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;
}
}
}
}