Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Hal Vaughan - 03 Oct 2006 15:40 GMT
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
Patricia Shanahan - 03 Oct 2006 17:28 GMT
...
> 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
Hal Vaughan - 03 Oct 2006 17:53 GMT
> ...
>> Is there some way I can return a String[][], then if I only need one
[quoted text clipped - 11 lines]
>
> 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
Patricia Shanahan - 04 Oct 2006 06:39 GMT
>> ...
>>> Is there some way I can return a String[][], then if I only need one
[quoted text clipped - 23 lines]
>
> 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
François Grondin - 03 Oct 2006 17:35 GMT
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

> 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
[quoted text clipped - 24 lines]
>
> Hal


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.