排序ArrayList中不显示? [英] Sorted ArrayList not displaying?
问题描述
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));我得到一个错误无法找到符号,当我运行程序不显示。我看到的是这样的:
For this line mainTextArea.setText(title2 + collection.get(j)); I get an error "cannot find symbol" and it doesn't display when I run the program. All I see is this:
原始订单
renier
MERT
雨
mylen
DREB
renier
mert
rain
mylen
dreb
推荐答案
您已经定义了集
像字符串字符串集合=;
。有 GET(J)
中的String类被称为没有一种方法。尝试使用ArrayList对象 cdcollection
。还拿创作的ArrayList的。你已经做过类似的ArrayList&LT;串GT; cdcollection =新的ArrayList();
,尽量使用泛型的两侧如的ArrayList&LT;串GT; cdcollection =新的ArrayList&LT;串GT;();
,否则你可能会得到不必要的警告
You have defined collection
as a string like String collection = "";
. There is no method called get(j)
in String class. Try to use your arraylist object cdcollection
. Also take of the creation of arraylist. You have done like ArrayList <String> cdcollection = new ArrayList();
, try to use generics both the sides like ArrayList <String> cdcollection = new ArrayList<String>();
, otherwise you might get unnecessary warnings.
编辑:
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);
}
这篇关于排序ArrayList中不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!