2012-10-28 29 views
0

我有一個由列和行組成的二維數組,當它計算出它構建矩陣或矩陣中的值時,問題是我的文本區域是隻顯示我的最終結果在我的2D數組,而不是所有其他的,但在我的控制檯在NetBeans它顯示所有的值我怎麼可能改變我的代碼來啓用this.Below是我認爲是問題的片斷。謝謝爲什麼我不能在我的文本區域中顯示我的二維數組的值

這是我actionperform按鈕顯示

 for (int i =0; i < rows; i++) { 
     for (int j = 0; j < cols; j++) { 
     matrix_tf.setText(String.valueOf(matrix[i][j])); 

,這是代碼來計算我的矩陣

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; 
      } 
     } 
    } 
} 

回答

0

在這個循環中:

for (int i =0; i < rows; i++) { 
    for (int j = 0; j < cols; j++) { 
    matrix_tf.setText(String.valueOf(matrix[i][j])); 

設置您每次迭代的字段文本(設置文本覆蓋了公司之一)。嘗試串聯您的文本:如果您使用的是文本區域(而不是文本字段)

for (int i =0; i < rows; i++) { 
    for (int j = 0; j < cols; j++) { 
    matrix_tf.setText(matrix_tf.getText() + " " + String.valueOf(matrix[i][j])); 

使用追加爲@Sujay建議

+0

由於沒有意識到這是簡單的東西複雜的區域....謝謝你清除它 –

+0

這工作,但是是一個非常低效的建立一個長字符串的方式。 –

+0

同意。我只是在說一個觀點。這裏肯定應該使用StringBuilder。 – giorashc

0

正如它的名字和its javadoc表示,setText()設置文本的文本給定String參數的區域。它不附加文字。

使用StringBuilder來連接各個矩陣元素爲字符串,並設定了文本區的完整結果的文本:像在想以上

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(' '); 
    } 
    sb.append('\n'); 
} 
textArea.setText(sb.toString()); 
相關問題