简体   繁体   English

为什么我无法在文本区域中显示二维数组的值

[英]Why can't I display values of my 2d array in my text area

I have a 2d array made up of columns and rows and when computed it builds a matrix or the values in the matrix but the problem is that my text area is displaying only my final result in my 2d array and not all the others but in my console in netbeans it does display all the values How may I change my code to enable this.Below are the pieces that I think is where the problem is coming. 我有一个由列和行组成的2d数组,计算时会建立一个矩阵或矩阵中的值,但问题是我的文本区域仅在2d数组中显示最终结果,而在我的2d数组中未显示所有其他结果在netbeans控制台中,它确实显示了所有值。如何更改代码以启用此功能。以下是我认为问题所在的部分。 Thank you 谢谢

This is in my actionperform button to display the 这是我actionperform按钮显示

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

and this is the code to compute my matrix 这是计算矩阵的代码

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

In this loop : 在此循环中:

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

you set the text of your field on each iteration (setting the text overwrites the prvioues one). 设置字段的文本在每次迭代(设置文本覆盖prvioues一个)。 Try concatenating your texts : 尝试串联文本:

 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]));

If you are using a text area (and not a text field) use append as @Sujay suggested 如果使用的是文本区域(而不是文本字段),请使用append作为@Sujay建议

As its name and its javadoc indicates, setText() sets the text of the text area to the given String argument. 正如其名称及其javadoc所指示的那样, setText()将文本区域的文本设置为给定的String参数。 It doesn't append the text. 它不会附加文本。

Use a StringBuilder to concatenate the various matrix elements to a String, and the set the text of the text area with the complete result: 使用StringBuilder将各种矩阵元素连接为String,然后使用完整的结果设置文本区域的文本:

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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM