2D String[][] to String[]?

H

Hal Vaughan

I have a class that reads data from a table and returns a 2D String[][].
There are a number of times when it'll only need one column of data, in
which case, a single array (String[]) would do. The method that will be
doing this has enough in it that might change that I don't want to write 2
different methods. I've considered the idea that at one point I can
combine the multiple column fields into a tab separated String, but then I
need to iterate through the first array a 2nd time and split each line.

Is there some way I can return a String[][], then if I only need one column
of data, to get just the first element of each secondary array? For
instance, get rowData[0][0], rowData[1][0], rowData[2][0], rowData[3][0],
without iterating through the entire array of data?

Every way I can think of means going through the table, getting the data,
and creating a 1D or 2D array, then having to iterate through it again to
convert if it's not what I need that time, or writing 2 separate methods.
Since this is part of the GUI, I want to keep it as fast as possible. I
considered using two objects, one String[] and one String[][], making them
global to that class and subclasses (which would help in other areas, as
well), calling the method to fill them with data, then reading whichever
one I need, but as I understand it, that's not thread safe and can result
in problems under Swing. One thread could call the method, fill up the
data, and try to read it while another thread is refilling it with
different data. (Or do I misunderstand that?)

Thanks for any suggestions, pointers, URLs, or help on this.

Hal
 
P

Patricia Shanahan

Hal Vaughan wrote:
....
Is there some way I can return a String[][], then if I only need one column
of data, to get just the first element of each secondary array? For
instance, get rowData[0][0], rowData[1][0], rowData[2][0], rowData[3][0],
without iterating through the entire array of data?
....

Could you transpose the String[][]? Use each element the array of arrays
to reference a column, rather than a row. Then the first column is just
element 0.

Whether this helps depends on the other uses of the array.

Patricia
 
F

François Grondin

Hi Hal.

You should have a look at the Colt package
(http://dsd.lbl.gov/~hoschek/colt/). The classes you should look for are
ObjectMatrix1D, ObjectMatrix2D, ObjectMatrix3D and all the classes that
reimplements these three classes.

They work like matrices, but are stored as Object[]. There are many useful
methods to navigate and define views on these matrices. This should be a
good starting point. If you don't plan to use Colt, it will give you ideas
how to solve your problem.

Bye.

Francois
 
H

Hal Vaughan

Patricia said:
Hal Vaughan wrote:
...
Is there some way I can return a String[][], then if I only need one
column
of data, to get just the first element of each secondary array? For
instance, get rowData[0][0], rowData[1][0], rowData[2][0], rowData[3][0],
without iterating through the entire array of data?
...

Could you transpose the String[][]? Use each element the array of arrays
to reference a column, rather than a row. Then the first column is just
element 0.

Whether this helps depends on the other uses of the array.

Patricia

Okay -- I'm so used to using X,Y (and I even go nuts if I don't use X,Y for
indices on 2D loops) that I never thought of swapping them.

Thanks for a good and simple idea! Then I could take the first array.

There's one other option I'm considering. When called, this method needs a
boolean flag to determine if it's multi-column or not. I may just return
an Object that could be a 1D array if the multi-column flag is false and a
2D array if it's true. This seems a little sloppy to me, though.

Hal
 
P

Patricia Shanahan

Hal said:
Patricia said:
Hal Vaughan wrote:
...
Is there some way I can return a String[][], then if I only need one
column
of data, to get just the first element of each secondary array? For
instance, get rowData[0][0], rowData[1][0], rowData[2][0], rowData[3][0],
without iterating through the entire array of data?
...

Could you transpose the String[][]? Use each element the array of arrays
to reference a column, rather than a row. Then the first column is just
element 0.

Whether this helps depends on the other uses of the array.

Patricia

Okay -- I'm so used to using X,Y (and I even go nuts if I don't use X,Y for
indices on 2D loops) that I never thought of swapping them.

Thanks for a good and simple idea! Then I could take the first array.

There's one other option I'm considering. When called, this method needs a
boolean flag to determine if it's multi-column or not. I may just return
an Object that could be a 1D array if the multi-column flag is false and a
2D array if it's true. This seems a little sloppy to me, though.

Hal

Note that if you transpose, you can always return an array of arrays,
but with only one element if that is all that is needed.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top