上記は私のアプリケーションのイメージです。2 つの文字列の位置合わせを行うアプリケーションを作成しています。2 つの文字列が比較され、2 次元配列が入力されます。2 次元配列の値は、行列の値になります。すべて私が持っている唯一の問題は、マトリックスの値を出力するときに、「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;
}
}
}
}