如何向 ARRAYLIST 添加数据

发布于 2024-10-10 12:49:10 字数 1468 浏览 1 评论 0原文

try {
    final List<String> ar = new ArrayList<String>();
    final PRIvariable pri = new PRIvariable();

    final BufferedReader reader = new BufferedReader(
        new InputStreamReader(new FileInputStream("C:/cdr2.csv")));

    while (reader.ready()) {
        final String line = reader.readLine();
        final String[] values = line.split(",");
        pri.dateText = values[2] + "    " + values[4];
        pri.count = pri.count + 1;
        pri.sum = pri.sum + Integer.parseInt(values[7]);
        System.out.println(pri.dateText + "  " + pri.sum + " " + pri.count);
        ar.add(pri);
    }

    final String[] columnNames = { "Date", "TOTAL", "COUNTS" };
    final String[][] cells = new String[ar.size()][3];
    for (int i = 0; i < ar.size(); i++) {
        cells[i][0] = ((PRIvariable) ar.get(i)).dateText;
        cells[i][1] = "" + ((PRIvariable) ar.get(i)).sum;
        cells[i][2] = "" + ((PRIvariable) ar.get(i)).count;
    }
    table = new JTable(cells, columnNames);
    table.setSize(400, 400);
    table.setVisible(true);
    final JScrollPane js = new JScrollPane();
    js.setViewportView(table);
    js.setSize(400, 400);
    js.setVisible(true);
    add(js, java.awt.BorderLayout.CENTER);

} catch (final Exception e) {
    System.out.println(e);
}

这是我的代码。这里我想读取文本文件并将该数据放入 Jtable 中。但在此代码中,它显示 Jtable 的每一行都填充了与 arraylist(ar) 最后一行中包含的相同数据。 (我认为我的数组列表有问题)。我该如何解决这个问题......

try {
    final List<String> ar = new ArrayList<String>();
    final PRIvariable pri = new PRIvariable();

    final BufferedReader reader = new BufferedReader(
        new InputStreamReader(new FileInputStream("C:/cdr2.csv")));

    while (reader.ready()) {
        final String line = reader.readLine();
        final String[] values = line.split(",");
        pri.dateText = values[2] + "    " + values[4];
        pri.count = pri.count + 1;
        pri.sum = pri.sum + Integer.parseInt(values[7]);
        System.out.println(pri.dateText + "  " + pri.sum + " " + pri.count);
        ar.add(pri);
    }

    final String[] columnNames = { "Date", "TOTAL", "COUNTS" };
    final String[][] cells = new String[ar.size()][3];
    for (int i = 0; i < ar.size(); i++) {
        cells[i][0] = ((PRIvariable) ar.get(i)).dateText;
        cells[i][1] = "" + ((PRIvariable) ar.get(i)).sum;
        cells[i][2] = "" + ((PRIvariable) ar.get(i)).count;
    }
    table = new JTable(cells, columnNames);
    table.setSize(400, 400);
    table.setVisible(true);
    final JScrollPane js = new JScrollPane();
    js.setViewportView(table);
    js.setSize(400, 400);
    js.setVisible(true);
    add(js, java.awt.BorderLayout.CENTER);

} catch (final Exception e) {
    System.out.println(e);
}

This is my code. Here i want to Read text file and put that data to Jtable. But in this code it shows every row of the Jtable filled with same data that contain in arraylist(ar) last row. ( i think there is problem in my arraylist). How can i solve this......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2024-10-17 12:49:10

问题出在变量 pri 上。它必须在 while 循环内创建。

像这样

String line = null;
while ((line = reader.readLine()) != null) {
    PRIvariable pri = new PRIvariable();
    String[] values = line.split(",");
    pri.dateText = values[2] + "    " + values[4];
    pri.count = pri.count + 1;
    pri.sum = pri.sum + Integer.parseInt(values[7]);
    System.out.println(pri.dateText + "  " + pri.sum + " " + pri.count);
    ar.add(pri);
}

在您的代码中,您希望为文件中的每一行创建一个单独的 PRIvariable 实例,但您在开始时只创建一次 PRIvariable 实例,那么您就是始终通过覆盖先前的值来使用该实例。

The problem is with the variable pri. It has to be created inside the while loop.

Like this

String line = null;
while ((line = reader.readLine()) != null) {
    PRIvariable pri = new PRIvariable();
    String[] values = line.split(",");
    pri.dateText = values[2] + "    " + values[4];
    pri.count = pri.count + 1;
    pri.sum = pri.sum + Integer.parseInt(values[7]);
    System.out.println(pri.dateText + "  " + pri.sum + " " + pri.count);
    ar.add(pri);
}

In your code your want to create a separate instance of PRIvariable for every line in the file, but you are creating only once instance of PRIvariable at the beginning then you are always using that instance by overriding the previous value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文