获取 csv 并比较行。数组列表?爪哇
我不经常使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要提前知道行大小,String.split() 返回一个字符串数组:
访问特定行:
另外,您是否总是按除最后一列之外的整行进行比较?您可以去掉最后一个值(
line.replaceFirst(",.*?$", "")
)并将行作为字符串进行比较(必须小心空格和其他格式,课程)。稍微不同的方式:
当然,如果您确实需要捕获匹配线,请根据需要进行修改。
You don't need to know the row size in advance, String.split() returns a String array:
To access a specific row:
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:
Of course, modify as necessary if you actually need to capture the matching lines.
您需要声明一个数组的 ArrayList。假设 csv 文件具有已知的列数,这里需要的唯一动态列表是“表”的“行”,由数组 char[] (列)的 ArrayList(行)组成。 (如果没有,那么 ArrayList 的 ArrayList 就可以了)。
它就像任何其他语言中的二维表:数组的数组。只是在这种情况下,其中一个数组需要是动态的。
要读取该文件,您需要两个循环。一个读取每一行,就像您正在做的那样,另一个读取每个字符的字符。
快速说明一下:如果您要像这样声明一个数组:
然后将每一行添加到 ArrayList 中,如下所示:
您将拥有一个充满指向同一数组的指针的列表。您需要使用 .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:
and then going to add each row to the ArrayList like this:
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);