3 dimentional array how to get length of z axis

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
LVL 7
gudii9Asked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
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
 
dpearsonCommented:
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
 
ozoCommented:
What are the 3 dimensions of the array, and which is z?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ozoCommented:
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
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
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

Open in new window


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

Open in new window



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
 
gudii9Author Commented:
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
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
ozoCommented:
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
 
gudii9Author Commented:
 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

please advise
0
 
ozoCommented:
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
 
gudii9Author Commented:
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
All Courses

From novice to tech pro — start learning today.