javawocky
javawocky

Reputation: 919

Putting values into a two-dimensional array from an Excell sheet

I have an excell sheet with several colums of values and want to put them in a 2D array. Normally, the way I would do it would be:

int[][] example={{colum 1 values},{colum 2 values},{colum 3 values}};

This way works fine, but can be very time consuming when I have lots of colums. Is there a faster way to do this?

Note: This is being used in an exercise I'm doing, so I dont want to stray into using XML or anything like that just yet.

Upvotes: 0

Views: 828

Answers (1)

StormeHawke
StormeHawke

Reputation: 6207

Without seeing code nobody can really give you specific examples, but the algorithm is pretty simple.

First initialize the outer size of your 2d array. Then you just iterate over your columns in a loop and add the values.

Now, that said, why are you not using a more convenient data structure, such as a List of Lists? (ie List<List<String>>). I would suggest that because it takes all the array management out of the equation.

Note that the code below is somewhat pseudo-code-ish given that we don't know what objects you're working with

List<List<String>> table = new ArrayList<List<String>>();

//Note that you have to initialize each list inside the list
for(int i = 0; i < columns.size; i++)
{
    table.add(new ArrayList<String>());
}

then you can just add values to each list:

table.get(columnIndex).add(value);

or

table.get(columnIndex).addAll(listOfValues);

Upvotes: 1

Related Questions