Solved

3 dimentional array how to get length of z axis

Posted on 2014-10-30
11
151 Views
Last Modified: 2014-11-04
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
Comment
Question by:gudii9
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 26

Expert Comment

by:dpearson
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

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

Expert Comment

by:ozo
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 35

Assisted Solution

by:mccarl
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

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
 
LVL 7

Author Comment

by:gudii9
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 35

Expert Comment

by:mccarl
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

by:ozo
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

by:gudii9
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

please advise
0
 
LVL 84

Expert Comment

by:ozo
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

by:gudii9
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

by:
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 64
Not needed 13 95
Windows 10 and JDK 7 7u85 4 33
servlet filter example 37 46
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now