0

这就是我所拥有的

ArrayList <String> cdList = new ArrayList();
Collections.addAll(cdList, "ExampleA\n"+"ExampleB\n"+"ExampleC\n"+"ExampleD");

Collections.sort(cdList, String.CASE_INSENSITIVE_ORDER);

System.out.println(cdList);


    bigBox.setText("Original Order\n**************\n");


    for (int i = 0; i < cdList.size(); i++)  {
    bigBox.setText(bigBox.getText()+""+cdList.get(i)+"\n");
    }

    bigBox.setText(bigBox.getText()+"\n\nSorted Order\n************\n");
    Collections.sort(cdList);


    for (int j = 0; j < cdList.size(); j++)  {
    bigBox.setText(bigBox.getText()+""+);
    }

我希望按原始顺序和字母顺序输出 4 个示例。我究竟做错了什么?

4

2 回答 2

1

您只将一个元素(字符串)添加到列表中,即一个连接字符串。

改变这个

ArrayList <String> cdList = new ArrayList();
Collections.addAll(cdList, "ExampleA\n"+"ExampleB\n"+"ExampleC\n"+"ExampleD");

List <String> cdList = new ArrayList<String>();
Collections.addAll(cdList, "ExampleA","ExampleB","ExampleC","ExampleD");

阅读更多收藏#addAll

为了显示你应该使用append而不是setText.

例子:

bigBox.append("Original Order\n**************\n");
for (String s : cdList)  {
 bigBox.append(s);
 bigBox.append("\n");
}
于 2013-11-12T16:50:31.470 回答
0

我假设您的元素是字符串"ExampleA", "ExampleB","ExampleC""ExampleD". 如果是这种情况,您当前在调用中所做的是将它们作为一个长字符串Collections.addAll()添加到 cdList 中。运算符在用于字符串时会附加它们。您可能希望它用逗号分隔它们,而不是:+

Collections.addAll(cdList, "ExampleA\n"+"ExampleB\n"+"ExampleC\n"+"ExampleD");

你有:

Collections.addAll(cdList, "ExampleA\n", "ExampleB\n", "ExampleC\n", "ExampleD");
于 2013-11-12T16:52:37.083 回答