0
ArrayList <String> cdcollection = new ArrayList();

private void initButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Collections.addAll(cdcollection, "renier\n",  "mert\n", "rain\n", "mylen", "dreb\n"    );
    initButton.setEnabled(false);
}

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

    String title1 = "Original Order\n\n";
    String title2 = "Sorted Order\n\n";

    String collection = "";
    for (int i = 0; i < cdcollection.size(); i++)  {
        collection = collection + cdcollection.get(i);
    }
    mainTextArea.setText(title1 + collection);

    Collections.sort(cdcollection);

    for (int j = 0; j < cdcollection.size(); j++)  {
        mainTextArea.setText(title2 + collection.get(j));
    }
}

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
    cdcollection.add(cdtitleInput.getText());
}

private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    cdcollection.remove(cdcollection.size()-1);
}

对于这一行 mainTextArea.setText(title2 + collection.get(j)); 我收到一个错误“找不到符号”,并且在我运行程序时它不显示。我所看到的只有这样:

原始订单


雷尼尔·梅特 ·雷恩·
迈伦 ·德雷布

4

1 回答 1

0

您已定义collection为一个字符串,如String collection = "";. get(j)String 类中没有调用方法。尝试使用您的 arraylist 对象cdcollection。还承担了arraylist的创建。您已经完成了 like ArrayList <String> cdcollection = new ArrayList();,尝试在 like 两边都使用泛型ArrayList <String> cdcollection = new ArrayList<String>();,否则您可能会收到不必要的警告。

编辑:

public static void main(String[] args){
    ArrayList<String> cdcollection = new ArrayList();
    Collections.addAll(cdcollection, "renier\n", "mert\n", "rain\n",
            "mylen", "dreb\n");

    String title1 = "Original Order\n\n";
    String title2 = "Sorted Order\n\n";

    String collection = "";
    for (int i = 0; i < cdcollection.size(); i++) {
        collection = collection + cdcollection.get(i);
    }
    System.out.println(title1 + collection);

    Collections.sort(cdcollection);
    System.out.println(cdcollection);
}
于 2012-05-21T21:07:11.773 回答