How to convert 2D ArrayList to String?

965 Views Asked by At

I am trying to convert values of a 2D ArrayList to a string to that I can print them onto a JTextArea. However, everytime I run my program, the 2D ArrayList is still in the square brackets. Does anyone know a fix for this?

private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {

    for (int row = 0; row <= count; row++) {

        employeeDisplay.setText(String.valueOf(employeeRecords.get(row)));

    }
}
2

There are 2 best solutions below

2
PeaceIsPearl On BEST ANSWER

Try this in your for loop :

StringBuilder builder = new StringBuilder();
for (String value : employeeRecords.get(row)) {
    builder.append(value);
}
String text = builder.toString();
employeeDisplay.setText(text);

OR

String formatedString = employeeRecords.get(row).toString()
    .replace(",", "")  //remove the commas
    .replace("[", "")  //remove the right bracket
    .replace("]", "")  //remove the left bracket
    .trim();  
employeeDisplay.setText(formatedString);
0
Yassin Hajaj On

If you're using , you could use Collectors#joining

employeeDisplay.setText(employeeRecords.get(row)
                                       .stream()
                                       .collect(Collectors.joining(" "));