I have a table of data (the number of columns can vary in length on different rows). I also need to be able to delete or add new rows of data.
What is the best way to store this data?
My first guess would be an ArrayList
.
I have a table of data (the number of columns can vary in length on different rows). I also need to be able to delete or add new rows of data.
What is the best way to store this data?
My first guess would be an ArrayList
.
Two approaches:
Convert everything to strings and use an ArrayList<List<String>>
where each entry is a row represented by an ArrayList<String>
.
As dystroy said, create a class representing a Row
in the table, and use an ArrayList<Row>
row.getDate()
instead of row.get(3)
).I'd choose LinkedList especially if you expect your list to work as a Stack.
Main Drawback of ArrayList is that this one recreates a larger table when capacity is reached => table allocation and copy gets performance slower.
Whereas with LinkedList, there is no concept of capacity since all works by pointers.
According to me, the main (and probably unique in mostly cases...) reason to prefer ArrayList rather than LinkedList is when you mainly want to access (read part so) a particular index. With ArrayList it is O(1), whereas with LinkedList it is O(n).
You can read this post for more information :