获取 csv 并比较行。数组列表?爪哇

发布于 2024-10-03 07:18:39 字数 808 浏览 1 评论 0原文

我不经常使用java,现在我遇到了一些问题。 我想读取这样的 CSV 文件:

A、B、C、D
A、B、F、K
E、F、S、A
A、B、C、S
A、C、C、S

Java不知道动态数组,所以我选择ArrayList。到目前为止这有效。问题是: 如何存储ArrayList?我认为另一个 ArrayList 会有帮助。 这就是我得到的:

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(this.getClass().getResourceAsStream(
                    "../data/" + filename + ".csv")));

    List rows = new ArrayList(); 

    String line;
    while ((line = reader.readLine()) != null) {

        rows.add(Arrays.asList(line.split(",")));

    }

现在我得到一个 rows.size() 大小为 5 的 ArrayList。 例如,如何获取行[0][0]?

我想做什么?问题是我想找到除最后一列之外的同一行。 例如我想找到第 0 行和第 3 行。 非常感谢

大家!你帮了我很多。 =) 也许 Java 和我会成为朋友 =) 谢谢!

i dont't use java very often and now i got some Problem.
I want to read a CSV file like this one:

A,B,C,D
A,B,F,K
E,F,S,A
A,B,C,S
A,C,C,S

Java don't know dynamic arrays, so i choose an ArrayList. This works so far. The Problem is:
How can I store the ArrayList? I think an other ArrayList would help.
This is what I got:

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(this.getClass().getResourceAsStream(
                    "../data/" + filename + ".csv")));

    List rows = new ArrayList(); 

    String line;
    while ((line = reader.readLine()) != null) {

        rows.add(Arrays.asList(line.split(",")));

    }

Now I get an ArrayList with a size of 5 for rows.size().
How do I get row[0][0] for example?

What do I want to do? The Problem is i want to find the same row except the last column.
For example i want to find row 0 and row 3.
thank you very much

Thank you all! You helped me a lot. =) Maybe Java and I will become friends =) THANKS!

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

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

发布评论

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

评论(2

骷髅 2024-10-10 07:18:39

您不需要提前知道行大小,String.split() 返回一个字符串数组:

List<String[]> rows = new ArrayList<String[]>(); 

String line = null;
while((line = reader.readLine()) != null)
    rows.add(line.split(",", -1));

访问特定行:

int len = rows.get(0).length;
String val = rows.get(0)[0];

另外,您是否总是按除最后一列之外的整行进行比较?您可以去掉最后一个值(line.replaceFirst(",.*?$", ""))并将行作为字符串进行比较(必须小心空格和其他格式,课程)。

稍微不同的方式:

Set<String> rows = new HashSet<String>(); 

String line = null;
while((line = reader.readLine()) != null){
    if(!rows.add(line.substring(0, line.lastIndexOf(','))))
        System.out.println("duplicate found: " + line);
}

当然,如果您确实需要捕获匹配线,请根据需要进行修改。

You don't need to know the row size in advance, String.split() returns a String array:

List<String[]> rows = new ArrayList<String[]>(); 

String line = null;
while((line = reader.readLine()) != null)
    rows.add(line.split(",", -1));

To access a specific row:

int len = rows.get(0).length;
String val = rows.get(0)[0];

Also, are you always comparing by the entire row except the last column? You could just take off the last value (line.replaceFirst(",.*?$", "")) and compare the rows as strings (have to be careful of whitespace and other formatting, of course).

A slightly different way:

Set<String> rows = new HashSet<String>(); 

String line = null;
while((line = reader.readLine()) != null){
    if(!rows.add(line.substring(0, line.lastIndexOf(','))))
        System.out.println("duplicate found: " + line);
}

Of course, modify as necessary if you actually need to capture the matching lines.

半葬歌 2024-10-10 07:18:39

您需要声明一个数组的 ArrayList。假设 csv 文件具有已知的列数,这里需要的唯一动态列表是“表”的“行”,由数组 char[] (列)的 ArrayList(行)组成。 (如果没有,那么 ArrayList 的 ArrayList 就可以了)。

它就像任何其他语言中的二维表:数组的数组。只是在这种情况下,其中一个数组需要是动态的。

要读取该文件,您需要两个循环。一个读取每一行,就像您正在做的那样,另一个读取每个字符的字符。

快速说明一下:如果您要像这样声明一个数组:

char[] row = new char[5];

然后将每一行添加到 ArrayList 中,如下所示:

yourList.add(row);

您将拥有一个充满指向同一数组的指针的列表。您需要使用 .clone() 方法,如下所示:

yourList.add(row.clone());

要像 table[1][2] 一样访问它,您需要使用 arraylist.get(1).get(2);

You'll need to declare an ArrayList of arrays. Asuming that csv file has a known number of columns, the only dynamic list needed here are the "rows" of your "table", formed by an ArrayList(rows) of arrays char[] (columns). (If not, then an ArrayList of ArrayList is fine).

It's just like a 2D table in any other language: an array of arrays. Just that in this case one of the arrays needs to be dynamic.

To read the file you'll need two loops. One that reads each line, just as you're doing, and another one that reads char per char.

Just a quick note: if you are going to declare an array like this:

char[] row = new char[5];

and then going to add each row to the ArrayList like this:

yourList.add(row);

You will have a list full of pointers to the same array. You'll need to use the .clone() method like this:

yourList.add(row.clone());

To access it like table[1][2], you'll need to use arraylist.get(1).get(2);

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