4

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
HarryB
  • 85
  • 1
  • 2
  • 5
  • A table of data conveys at *least* two dimensions. Were you thinking of a database or a `Map` instead? – Makoto May 02 '12 at 15:19
  • 2
    Create a Row class containing an array and use an ArrayList to contain your "table". Of course it depends on your use cases but only you can devise it properly for your needs. – Denys Séguret May 02 '12 at 15:20
  • use multidimensional array see this http://www.homeandlearn.co.uk/java/multi-dimensional_arrays.html – Zaz Gmy May 02 '12 at 15:20
  • Do all the columns have the same type? – trutheality May 02 '12 at 15:22
  • Multidimensional array don't make it simple to delete rows. But without the whole set of requirements (including the data, the types of contents, the variability of columns, etc.), we're not really going to find the solution. – Denys Séguret May 02 '12 at 15:22
  • @trutheality they are ints, doubles and Strings if thats what you mean but i can convert them all to Strings if it's easier. – HarryB May 02 '12 at 15:25
  • also lol @ negative votes, people obviously don't like the way i asked the question. – HarryB May 02 '12 at 15:27
  • If you have so delete operation on List, It'd better if you use `LinkedList` instead of `ArrayList`. As deletion on `ArrayList` is always overhead. cons of `LinkedList` is its slower than `ArrayList` on iteration. – Subhrajyoti Majumder May 02 '12 at 16:13

2 Answers2

6

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.
trutheality
  • 23,114
  • 6
  • 54
  • 68
1

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 :

When to use LinkedList over ArrayList?

Community
  • 1
  • 1
Mik378
  • 21,881
  • 15
  • 82
  • 180