我目前遇到的问题是,当我将数组输出到有序列表时,它只输出五个集合中的最后一首歌。任何帮助我如何使它显示所有五个将不胜感激。 (第一次尝试使用数组)。
private void initActionPerformed(java.awt.event.ActionEvent evt) {
ArrayList songTitle = new ArrayList();
Collections.addAll(songTitle, "Pink Floyd - The Dark Side of the Moon", "AC/DC - Back in Black", "Led Zeppelin - Led Zeppelin IV", "Billy Joel - Piano Man", "Eric Clapton - Unplugged");
Collections.sort(songTitle);
for (int j = 0; j < songTitle.size(); j++) {
nameOutput.setText(j + "- " + songTitle.get(j));
}
}
答案 0 :(得分:3)
你在这里使用setText覆盖
for (int j = 0; j < songTitle.size(); j++) {
nameOutput.setText(j + "- " + songTitle.get(j));
}
首先附加字符串,然后设置为nameOutput
。
答案 1 :(得分:0)
您正在使用数组,但每次循环时似乎都会覆盖nameOutput
的内容。
答案 2 :(得分:0)
我相信你会期待这样的事情。 ?
StringBuilder sb = new StringBuilder();
sb.append("");
for (int j = 0; j < songTitle.size(); j++) {
sb.append(j);
sb.append("- ");
sb.append(songTitle.get(j));
sb.append(" ");
}
nameOutput.setText(sb.toString());