Solved

# 3 dimentional array how to get length of z axis

Posted on 2014-10-30
166 Views
if 3 dimentional array how to get length of z axis(elements in z axis).

Any of below three works right.
data[][0].length
data[][1].length
data[][2].length
0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2
• +1

LVL 28

Expert Comment

ID: 40414472
I'm not quite sure what question you're asking.  If all 3 are working, what is the problem?

Those methods will work depending on exactly how the 3 dimensional array is created (there are some choices).
0

LVL 84

Expert Comment

ID: 40414554
What are the 3 dimensions of the array, and which is z?
0

LVL 84

Expert Comment

ID: 40414574
If the the array is indexed as data[x][y][z], and data.length is at least 1, and data[0].length is at least 1, then I would use data[0][0].length
if data[0].length is greater than 2, then data[0][1].length and data[0][2].length could also work
0

LVL 36

Assisted Solution

mccarl earned 250 total points
ID: 40414719
With a 3 dimensional array like you are referring to, in Java, the 3 dimensions do not have to be consistent, ie. if you think of it like a 3d object, it is NOT a normal rectangular prism.

Or another way to put it, is that the length of (what you are calling) the z axis, can be different at every different value of x and y.

Or if you consider the easier 2d situation first, the array doesn't need to be a regular rectangle of values, ie. the array DOES NOT have to look like this...
``````1 2 3 4 5
9 8 7 6 5
1 3 5 7 9
``````

It is perfectly fine and valid to have a 2d array that looks like this
``````1 2 3 4 5
9 8 7
1 3 5 7 9 2 4 6 8 0
``````

Now if you extend that to a 3rd dimension, (say there are values coming OUT of the page for each element above), then the number of elements (or the height coming out of the page) can be different for every different element above. (**And no, I'm not going to try and draw that! ;)
0

LVL 7

Author Comment

ID: 40419753
data[x][y][z]

any option to get all three lenghts(x, y and z axis lengths) using all 3 set of brackekes(first set[] , second set [], third set []) next to data to be consistent and easy to understand as below similar to how we get individual cell element.

1 2 3 4 5
9 8 7 6 5
1 3 5 7 9
3 5 6 7 8

data[x][][].length--->gives lenght of x axis elements( 4 in above case)
data[][y][].length--->gives lenght of x axis elements( 5 in above case)
data[][][z].length--->gives lenght of x axis elements( no symmetric prism example i can draw here)

not sure on  non symmetric examples like below where number of row element length varying from each row but i still fee data[x][][].length should give 3 as there are three rows eventhough second row only has 3 elements compared to first row which has 5 elements)
1 2 3 4 5
9 8 7
1 3 5 7 9 2 4 6 8 0
0

LVL 36

Expert Comment

ID: 40420592
As for the non symmetric example, this is what the various length fields would return...

data.length = 3               // Since there are 3 rows

data[0].length = 5          // Since there are 5 elements in the first row (row 0)
data[1].length = 3          // Since there are 3 elements in the second row (row 1)
data[2].length = 10       // Since there are 10 elements in the third row (row 2)

Hopefully the above demonstrates why your idea of data[x][][].length (and the others) would not make much sense and why you need to be able to explicitly ask for which .length value that you want to query.
0

LVL 84

Expert Comment

ID: 40420794
data[x][][].length
is not valid syntax

data[x].length
returns a value only when x <  data.length

data[][y][].length
is not valid syntax

data[x][y].length
returns a value only when  x <  data.length && y < data[x].length
0

LVL 7

Author Comment

ID: 40421796
data[x][y].length
returns a value only when  x <  data.length && y < data[x].length

can you please elaborate on above

data[1][1].length what it returns
data[1][2].length what it returns
data[0][1].length what it returns

for below non symmetric example

1 2 3 4 5
9 8 7
1 3 5 7 9 2 4 6 8 0

0

LVL 84

Expert Comment

ID: 40421859
String[][] data = {
{"1", "two", "3", "4", "5"},
{"9", "8", "7"},
{"1", "3", "5", "7", "9", "2", "4", "6", "8", "0"}
};
System.out.println("data[1][1] is \"" + data[1][1] + "\"");
System.out.println("data[1][1].length() is " + data[1][1].length());
System.out.println("data[1][2] is \"" + data[1][2] + "\"");
System.out.println("data[1][2].length() is " + data[1][1].length());
System.out.println("data[0][1] is \"" + data[0][1] + "\"");
System.out.println("data[0][1].length() is " + data[0][1].length());

data[1][1] is "8"
data[1][1].length() is 1
data[1][2] is "7"
data[1][2].length() is 1
data[0][1] is "two"
data[0][1].length() is 3
0

LVL 7

Author Comment

ID: 40422209
data[0][1] is "two"
data[0][1].length() is 3

This one is interesting and makes sense now.
Can there be a another array or arraylist in place of string "two" . Please advise
0

LVL 84

Accepted Solution

ozo earned 250 total points
ID: 40422236
String[][] data
declares that data[0][1] is a String
If data was declared differently, then data[0][1] could be of a different type.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various â€¦
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month3 days, 21 hours left to enroll