简体   繁体   中英

Display not filled 2D array into JTextarea in java

I want to display array elements in the "jTextArea1".My 2D array is not a completely filled one.it is like this. row_count = 4 and column_count =5.

2D array:

    4 8 8 4 5
    2 7 5 6 
    6 4 1 8
    9 5 5 2

for(int r=0;r<row_count;r++)
{
    for(int c=0;c<column_count;c++)
    {
        jTextArea1.setText(Integer.toString(newArray[r][c]));

    }
    System.out.println("\n\n");
}

Out put of the above code was just zero. what is the error of this code? thanks.

#

 0

you overwrite every time the text by doing:

jTextArea1.setText(Integer.toString(newArray[r][c]));

consider using the helpfull Arrays method deepToString and you will get rid off the nested loops :)

System.out.println(Arrays.deepToString(x).replace("[", "").replace("]", ""));

so you can use:

jTextArea1.setText(Arrays.deepToString(newArray).replace("[", "").replace("]", ""));

You overwrite the TextArea input with setText() . Try TextArea1.append() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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