简体   繁体   中英

How to convert 2D ArrayList to String?

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

    }
}

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

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

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

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