Debugging multi-array

L

lielar

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?
 
C

Chris

lielar said:
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?
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.
 
L

Lew

Chris said:
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
 

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,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top