Java 中的错误:类型不兼容:LinkedList<Diff> 无法转换为 String

Error in Java: incompatible types: LinkedList<Diff> cannot be converted to String

您好,我需要帮助解决这个错误,提前感谢您的时间和考虑

this.txt_absent.setText(diff);行是出现错误的地方,我想显示diff在一个 JTextArea

这是我的代码: (这是一个 ActionPerformed JButton

    private void compareActionPerformed(java.awt.event.ActionEvent evt) {                                        
    diff_match_patch dmp = new diff_match_patch();
   
    LinkedList <diff_match_patch.Diff> diff = dmp.diff_main(txt_guide.getText() , txt_result.getText());
    dmp.diff_cleanupSemantic(diff); 
    
    this.txt_absent.setText(diff);
    } 

JTextArea 不接受 setText 方法中的 LinkedList 实例。

您需要以任何方便的方式将链表转换为适当的字符串。

最简单的方法是调用 LinkedList::toString:

this.txt_absent.setText(diff.toString());

或者可以使用 "\n" 分隔符连接链表中的元素,前提是 LinkedList 实现 Iterable:

this.txt_absent.setText(String.join("\n", diff));

或者实现自定义方法从链表构建字符串。