Java – a data structure for saving the contents of the parsed CSV file
I'm trying to find the best way to parse CSV files in Java Now each line has x messages For example, the first line can contain up to 5 string words separated by commas, while the next few lines may have 3 or 6 or more
My problem is not reading strings from files Just to be clear My question is what data structure is best to keep each row and each word in that row?
At first I thought about using 2D arrays, but the problem is that the array size must be static (the second index size will contain how many words are in each row, which may be different from row to row)
These are the first few lines of the CSV file:
0,MONEY 1,SELLING 2,DESIGNING 3,MAKING DIRECTOR,3DENT95VGY,EBAD,SAGHAR,MALE,05/31/2011,null,10000,07/24/2011 3KEET95TGY,04/17/2012,120050 3LERT9RVGY,03/05/2013,132500 3MEFT95VGY,145205 DIRECTOR,XKQ84P6CDW,AGHA,ZAIN,FEMALE,06/06/2011,1,1000,01/25/2012 XK4P6CDW,09/28/2012,105000 XKQ8P6CW,130900 DIRECTOR,YGUSBQK377,AYOUB,GRAMPS,10/02/2001,12/17/2007,2,12000,01/15/2002
Solution
You can use map < integer, list < string > > The key is the line number in the CSV file, and the list is the word in each line
Another point: you may often use the list #get (int) method If this is the case, do not use the link list This is because the get (int) of the linked list is O (n) I think ArrayList is your best choice
Edit (based on Alex Wien's observations):
In this particular case, because the key is a line number, a continuous set of integers is generated. A better data structure can be ArrayList < ArrayList < string > > This will result in faster key retrieval