Java - 拆分ArrayList

时间:2015-01-05 18:42:25

标签: java arraylist

大家好我正在尝试扫描文本文件的底部6行并在JOptionPane.showMessageDialog中显示它们当前显示为[line7, line6, line5, line4, line3, line2]我希望它显示为垂直列表而不是逗号分隔符。

ArrayList<String> bandWidth = new ArrayList<String>();
FileInputStream in = null;
try {
    in = new FileInputStream("src/list.txt");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String tmp;
try {
    while ((tmp = br.readLine()) != null)
    {
        bandWidth.add(tmp);
        if (bandWidth.size() == 7)
            bandWidth.remove(0);
    }
} catch (IOException e) {   
    e.printStackTrace();
}
ArrayList<String> reversedSix = new ArrayList<String>();
for (int i = bandWidth.size() - 1; i >= 0; i--)
    reversedSix.add(bandWidth.get(i));
                                                            JOptionPane.showMessageDialog(null,bandWidth,null,JOptionPane.INFORMATION_MESSAGE);

1 个答案:

答案 0 :(得分:3)

尝试循环ArrayList并生成带有换行符的String

String result = "";
for (int i = 0; i < bandWidth.size(); i++)
{
    result += bandWidth.get(i) + "\n";
}
JOptionPane.showMessageDialog(null,result ,null,JOptionPane.INFORMATION_MESSAGE);

注意:如果这是输出HTML,请使用<br \>代替\n