HarryB HarryB - 16 days ago 5
Java Question

Best way to store a table of data

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
.

Answer

Two approaches:

  1. Convert everything to strings and use an ArrayList<List<String>> where each entry is a row represented by an ArrayList<String>.

    • Advantage: Don't need to create your own class to represent a "row".
    • Disadvantage: Need to convert data, can't do mathematical operations without converting data back, need to make sure all rows are the same length.

  2. As dystroy said, create a class representing a Row in the table, and use an ArrayList<Row>

    • Advantage: entries keep their actual types, rows don't have variable lengths (unless you want them to), and you can have meaningful ways to access columns (e.g. row.getDate() instead of row.get(3) ).
    • Disadvantage: might be more work to create the additional class.