gudii9
asked on
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
Any of below three works right.
data[][0].length
data[][1].length
data[][2].length
What are the 3 dimensions of the array, and which is z?
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
if data[0].length is greater than 2, then data[0][1].length and data[0][2].length could also work
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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--->give
data[][y][].length--->give
data[][][z].length--->give
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
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.
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.
data[x][][].lengthis not valid syntax
data[x].length
returns a value only when x < data.length
data[][y][].lengthis not valid syntax
data[x][y].length
returns a value only when x < data.length && y < data[x].length
ASKER
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
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].lengt h() is " + data[1][1].length());
System.out.println("data[1 ][2] is \"" + data[1][2] + "\"");
System.out.println("data[1 ][2].lengt h() is " + data[1][1].length());
System.out.println("data[0 ][1] is \"" + data[0][1] + "\"");
System.out.println("data[0 ][1].lengt h() 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
{"1", "two", "3", "4", "5"},
{"9", "8", "7"},
{"1", "3", "5", "7", "9", "2", "4", "6", "8", "0"}
};
System.out.println("data[1
System.out.println("data[1
System.out.println("data[1
System.out.println("data[1
System.out.println("data[0
System.out.println("data[0
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
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Those methods will work depending on exactly how the 3 dimensional array is created (there are some choices).