简体   繁体   中英

Get csv and compare lines. ArrayList? 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!

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM