javaswingarraylistmultidimensional-arraynetbeans-6.9

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

    }
}

Solution

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