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 / March 2008

Tip: Looking for answers? Try searching our database.

Debugging multi-array

Thread view: 
lielar - 09 Mar 2008 12:22 GMT
Hi

I have the following multi-array, I don't know how to follow it.

        String [][][] arr = {
                {{}, null}, {{"1", "2"}, {"1", null, "3"}},
                {}, {{"1", null}}

        };

I have problems knowing what is the first, second, third objects. And
it being an array of an array of an array(3x), if one value is not
specified, how can you tell?
Chris - 09 Mar 2008 21:08 GMT
> Hi
>
[quoted text clipped - 9 lines]
> it being an array of an array of an array(3x), if one value is not
> specified, how can you tell?

 The first index refers to the outer array, the next to the next inner
array, etc.

If you don't know in advance how long each array is, then you must check
 lengths before accessing an element.

For example, to access arr[1][2][3], do this:

if (arr.length >= 2) {
    String [][] arr0 = arr[1];
    if (arr0.length >= 3) {
        String [] arr1 = arr0[2];
        if (arr1.length >= 4) {
            String arr2 = arr1[3];
            // use the value here
        }
    }
}

This is ugly. I'm sure you can find a more efficient way to do it.
Lew - 09 Mar 2008 23:04 GMT
> If you don't know in advance how long each array is, then you must check
>  lengths before accessing an element.

Worse, you have to check for null, too.

> For example, to access arr[1][2][3], do this:

 if ( arr != null && arr.length >= 2) {
>     String [][] arr0 = arr[1];
     if ( arr0 != null && arr0.length >= 3) {
>         String [] arr1 = arr0[2];
         if ( arr1 != null && arr1.length >= 4) {
>             String arr2 = arr1[3];
>             // use the value here
             // checking for null
>         }
>     }
> }
>
> This is ugly. I'm sure you can find a more efficient way to do it.

Signature

Lew



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



©2008 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.