Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a xlsx file, that has some tabs with different data. I want to be able to save each row of a tab in a list. The first thing that comes to mind is a list of lists, but I was wondering if there is another way. I'd like to save that information in a object, with all its benefits, but can't think of a way to generate/create such diverse objects on the fly. The data in the xlsx is diverse and ideally the program is agnostic of any content.

So instead of e.g. create a list for each row, than put that list in another list for each tab and each tab in another list, I'd like to store the information that each row represents in a single object and just have a list of different objects.

The question can be summarized as : What is an alternative to list of lists ?

A small graphic to visualize the problem :

|LIST                                                                |
|                                                                    |
|    +------------------+ +------------------+ +-----------------+   |
|    | Class1           | | Class2           | | Class3          |   |
|    |------------------| |------------------| |-----------------|   |
|    | var1             | | var1             | | var5            |   |
|    | var2             | | var2             | | var6            |   |
|... | var3             | |                  | | var7            |...|
|    |                  | |                  | |                 |   |
|    |                  | |                  | |                 |   |
|    |                  | |                  | |                 |   |
|    |                  | |                  | |                 |   |
|    +------------------+ +------------------+ +-----------------+   |
|                                                                    |
share|improve this question

closed as unclear what you're asking by Bart van Ingen Schenau, Robert Harvey, Kilian Foth, Dynamic, ChrisF Dec 11 '13 at 23:42

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Which benefits of objects do you hope to realize? Must you use java, or are other languages available? Have you looked at java.util.Map? – Aaron Kurtzhals Nov 27 '13 at 14:54
Flexibility among others. I think a list of objects is much better to work with than a list of lists of lists. Yes, I have to use java. Yes, but a map is another "list" ;) – cete3 Nov 27 '13 at 15:12
An Object seems less flexible than a Map. Can you make it clear exactly what flexibility you hope to gain? – Geo Nov 28 '13 at 0:37
After rethinking my problem, I will go with a variant of Olivier's approach. – cete3 Nov 29 '13 at 14:09
up vote 3 down vote accepted

I assume that your worksheets contain tabular data, i.e., the first row contains field names and the following rows contain values. In such a case it would make sense to define a Table class whose instances would each contain the contents of one worksheet. This Table object could hold general information regarding one worksheet, if necessary, like the name of the worksheet tab.

It could also have a list holding the column names or a dictionary holding the column indexes with the column names as a key. If you define a Column class for this purpose (instead of just a string for the column name), it allows you to store additional information regarding a column, like its type (numeric, text, date) and display format.

Even if you don't need this kind of infrastructure right now, having such classes allows you to add this kind of functionality much easier later.

The Table class would also contain a list of rows. Here again, having a Row class can have advantages. However, it would be ok to represent one row as an array of strings, for instance. Since each row of the table has the same length, a list is not necessary here.

Instead of using strings to store the values, having a Value class or struct can also have advantages. Such a type could automatically convert strings to an appropriate type, could perform comparisons between values of the same type etc.

Without knowing more about your data and the kind of logic you want to apply to it, it is difficult to give you an answer tailored to fit your needs.

share|improve this answer
Thank you Oliver for you answer, I like your approach and will give it some thought. – cete3 Nov 28 '13 at 7:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.