我目前遇到一个问题,当我将数组输出到有序列表时,它只输出五首歌曲集中的最后一首歌曲。任何有关如何使其显示所有五个的帮助将不胜感激。 (第一次尝试使用数组)。
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));
}
}
最佳答案
您正在此处使用 setText 进行覆盖
for (int j = 0; j < songTitle.size(); j++) {
nameOutput.setText(j + "- " + songTitle.get(j));
}
先附加字符串,然后设置为nameOutput
。
关于java - 程序仅输出数组列表中的最后一个元素 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37424457/