Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on multidimensional-array

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given these two statements:


because it would be correct to write:


without creating errors in compilation?
In my opinion it is not correct because the array b2, in that position, would require a 2-dimensional array and b is an array in 4 dimensions (each reference points to another array in 4 dimensions).
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
In my opinion it is not correct because the array b2, in that position, would require a 2-dimensional array and b is an array in 4 dimensions (each reference points to another array in 4 dimensions).


Yeah, it may help if you elaborate -- as I am not sure what you are trying to say here.

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that code would be correct if b was defined as:
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, b2 is an array of array of array of array of shorts. So, b2[1][1] is dereferencing that twice, getting you to an element that is an array of array of shorts. The b variable is defined as an array of array of shorts. Aren't the two types the same?

The size of the array doesn't determine the type. Both the b variable and the b2[1][1] element are array of array of shorts. So, why shouldn't the assignment be allowed?

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:In my opinion it is not correct because the array b2, in that position, would require a 2-dimensional array and b is an array in 4 dimensions (each reference points to another array in 4 dimensions).

Then your opinion is wrong! The above code is perfectly valid and will compile without errors

Array b2 is a 4D-array
=> b2[1][1] is a reference variable for a 2D array

Array b is a 2D-array
=> it's perfectly valid to assign b to b2[1][1]

Remember: it's the number of square bracket pairs ([]) that determines the number of dimensions, the number of elements in a dimension doesn't matter. Example:Array abc is a 2D-array (because you have 2 pairs of square brackets), it's not a 10D- nor 50D-array.

Hope it helps!
Kind regards,
Roel
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I understood.



- "b" array: it is a 2-dimensional array consisting of 2 other arrays, arrays are 4-dimensional array, each array refers an 4-dimensional array.
- "b2" array: it is a 4-dimensional array consisting up of 4 2-dimensional array, each array refers an array in 3 dimensions, which refers an 2-dimensional array that refers an further 2-dimensional array.

Is it correct?
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
- "b" array: it is a 2-dimensional array consisting of 2 other arrays, arrays are 4-dimensional array, each array refers an 4-dimensional array.
- "b2" array: it is a 4-dimensional array consisting up of 4 2-dimensional array, each array refers an array in 3 dimensions, which refers an 2-dimensional array that refers an further 2-dimensional array.


I think you are confusing the concept of the number of "dimensions" and the number of elements in the array.

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:- "b" array: it is a 2-dimensional array consisting of 2 other arrays, arrays are 4-dimensional array, each array refers an 4-dimensional array.
- "b2" array: it is a 4-dimensional array consisting up of 4 2-dimensional array, each array refers an array in 3 dimensions, which refers an 2-dimensional array that refers an further 2-dimensional array.

Is it correct?

No!

Array b is a 2D array. So it's a 1D array of (references to) 1D arrays. Array b contains 4 (references to) 1D arrays. Array b[0] contains 4 shorts (and the same applies to array b[1], b[2] and b[3]).

Array b2 is a 4D array. So it's a 1D array of (references to) 3D arrays. Array b2 contains 2 (references to) 3D arrays... (it's up to you to complete the remainder of this multi-dimensional array )
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to make a drawing to outline the concept.
is that correct?

scansione0001.jpg
[Thumbnail for scansione0001.jpg]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:I tried to make a drawing to outline the concept.
is that correct?

Yes, it is! Have a cow for making a drawing to better understand the concept.
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:I tried to make a drawing to outline the concept.
is that correct?


Yes.



Now, can you draw it after ... is executed?

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Now, can you draw it after ... is executed?

Henry


the problem is precisely that. ok, b is a 2-dimensional array as well as require at b2 [1] [1], the problem is that 2-dimensional array is composed of two arrays each with 2 elements, and b consists of two arrays from 4 elements.
I try to give me an answer.
In that case is not important the number of elements in the array because will have a reference to another array, then a memory address, the important thing is that it is a 2-dimensional reference.
Is it correct?
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
the problem is precisely that. ok, b is a 2-dimensional array as well as require at b2 [1] [1], the problem is that 2-dimensional array is composed of two arrays each with 2 elements, and b consists of two arrays from 4 elements.
I try to give me an answer.
In that case is not important the number of elements in the array because will have a reference to another array, then a memory address, the important thing is that it is a 2-dimensional reference.
Is it correct?


Give me a minute. I will draw the result for you... Attached

Henry
ArrayDrawing.jpg
[Thumbnail for ArrayDrawing.jpg]
Result of Array Assigment
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:In that case is not important the number of elements in the array because will have a reference to another array, then a memory address, the important thing is that it is a 2-dimensional reference.
Is it correct?

Spot-on! The only thing that matters is the dimension of the array, the number of elements doesn't matter at all. When you assign an array of a wrong dimension, you'll get a compiler error. Example:

And as you probably already know: every array is an object. And you can cast an object to an array as well. What do you think the output of this code will be (and why)?

Henry Wong wrote:Give me a minute. I will draw the result for you... Attached

Have a cow as well for so much artistic talent!
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
And as you probably already know: every array is an object. And you can cast an object to an array as well. What do you think the output of this code will be (and why)?




line 2 should be:


Is that correct? If it is correct, I believe that there will be an error in compile time rather than in runtime because the problem is on the reference.
after the change should not be any error and the output should be 4.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:line 2 should be:


Is that correct? If it is correct, I believe that there will be an error in compile time rather than in runtime because the problem is on the reference.
after the change should not be any error and the output should be 4.

No, unfortunately it's not correct!

Back to this post and repeat (again) after me: every array is an object, every array is an object, every array is an object! => line2 will compile without errors. In fact, the change you proposed (Object[][] o = arr1[0][1];) will result in a compiler error.

Do you fancy another attempt knowing that line2 is valid Java code?
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
line 2 should be:


Is that correct? If it is correct, I believe that there will be an error in compile time rather than in runtime because the problem is on the reference.
after the change should not be any error and the output should be 4.


No. It is not correct. The arr1 variable is an array of array of array of shorts. So, this would be correct...



Also, since a short array is an object, this would also be correct...



However, back to your original premise, a short array is most definitely *not* an array of array of Object instances.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
And as you probably already know: every array is an object. And you can cast an object to an array as well. What do you think the output of this code will be (and why)?


BTW, line three is not correct. And the cast should generate a cast error at runtime.

The arr1 variable is an array of array of array of shorts. So, dereferencing it twice should make it an array of shorts. Explicitly casting it to an array of array of shorts would throw an error/exception.

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the reference of type Object can have ONLY this form?


I think there is a mistake in the cast because it refers to an array of 3 short and instead the cast tries to turn it into an array of arrays of short, generating a ClassCastException at runtime.
The correct line would be:


and then the output will be 3

is that correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:But the reference of type Object can have ONLY this form?

Yes! And there is a simple reason for that: an array IS-A Object, but an array IS-NOT-A an array of Object

John Lerry wrote:I think there is a mistake in the cast because it refers to an array of 3 short and instead the cast tries to turn it into an array of arrays of short, generating a ClassCastException at runtime.
The correct line would be:


and then the output will be 3

is that correct?

That mistake was added intentionally (on purpose) Just wanted to see if you noticed the incompatible array dimensions between the reference variable and the actual object. And you did, so well done! And the original code results in a ClassCastException at runtime (message: [S cannot be cast to [[S) and when you change this code to the line you suggested, 3 will be printed.

Hope it helps!
Kind regards,
Roel
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even a single question. if the statement had been this:


there was an error in compilation? or the error would still been at runtime because that statement is syntactically correct?
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:or the error would still been at runtime because that statement is syntactically correct?


Well, does an array of short implicitly cast to an array of array of short? ... because that would be needed in order for that line to compile.

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when you have answered Henry's question, what do you think about these code snippets? Will they compile? Or do you get a runtime exception? Or ...?

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Lerry wrote:or the error would still been at runtime because that statement is syntactically correct?


Well, does an array of short implicitly cast to an array of array of short? ... because that would be needed in order for that line to compile.

Henry


I think the cast is actually needed because the type is the same (short) but the structure is different (array vs array of arrays), I hope I do not make mistakes.
Then the correct line should be:


Is that correct?

Roel De Nijs wrote:And when you have answered Henry's question, what do you think about these code snippets? Will they compile? Or do you get a runtime exception? Or ...?




snippet1 compile, snippet2 does not compile due to an error in compiling the line 5.
This error can be corrected by changing the education in this way:
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:I think the cast is actually needed because the type is the same (short) but the structure is different (array vs array of arrays), I hope I do not make mistakes.
Then the correct line should be:


Is that correct?

Not, is isn't. short[] and short[][] are incompatible types (like e.g. Integer and String), so short[] IS-NOT-A short[][], so you can't cast between them.

John Lerry wrote:snippet1 compile, snippet2 does not compile due to an error in compiling the line 5.

Unfortunately it's just the other way around: snippet1 does not compile (byte[] IS-NOT-A int[]), so you can't cast between them. But because String IS-A Object, String[] IS-A Object[] and you can cast a String[] to a [tt]Object without any problem at all (so you won't get a compiler error nor a runtime exception). Th code of snippet2 compiles and runs sucessfully.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now I have everything clear.


P.S. Could you give just look at this my thread ( OCP )? thank you.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you help me to understand the graphical representation of "array" in this code?



I can not understand how it is structured array.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Can you help me to understand the graphical representation of "array" in this code?

It's very similar to the drawings (you made) in earlier posts. The only difference: the number of elements is different for every 1D-array. Here's my drawing.

Hope it helps!
Kind regards,
Roel
array.jpg
[Thumbnail for array.jpg]
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanks for the clarification.

I now would like to know, given this code:


if this is a correct graphical representation of the array arr.
scansione0001.jpg
[Thumbnail for scansione0001.jpg]
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
if this is a correct graphical representation of the array arr.


No. At first glance, it is not even close. Give me a minute to draw it for you ...

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:if this is a correct graphical representation of the array arr.

Unfortunately it's not correct! Remember that a 3D-array is an 1D-array of (reference variables to) 2D-arrays. And a 2D-array is an 1D-array of (reference variables to) 1D-arrays, meaning you can't have String elements (like "a", "b",...) at this level. It should only contain reference variables (arraws to another array).

Have another try!
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Have another try!


Good idea. I finished the drawing (and I think it is correct... ), but I will hold off posting it, to let the OP try to figure it out.

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is correct this drawing? And also the consideration of the size and number of elements.
scansione0001.jpg
[Thumbnail for scansione0001.jpg]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Is correct this drawing? And also the consideration of the size and number of elements.

You are really close now! The 1st 3 elements of arr are correct, the 4th and 5th element are not. You probably made a slip-up with the curly braces near the end of the 3D-array because arr has only 4 elements (not 5).

You know what they say: third time's the charm!
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also remember that strings are objects, so the arrays hold references. It may be better to draw the strings outside of the arrays (boxes) with references (arrows) pointed towards them... but of course, this is my opinion, as the interpretation is understood.

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it correct this time?


p.s. when you can take a look here -> #4 Test 2 Enthuware
scansione0001.jpg
[Thumbnail for scansione0001.jpg]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Is it correct this time?

Almost!

2 small remarks:
  • {} (arr[3][1]) is an empty array, in your drawing it's an array with 1 element (I know it's hard to draw an empty array )
  • Like Henry already said in a previous post: String is an object, so the arrays at the bottom of your drawing don't contain strings but String reference variables. So to be 100% correct the array element should not contain the String value, but be an arrow which refers to the actual String value (unless it's null). But you probably have just drawn the shortcut version
  •  
    Henry Wong
    author
    Marshal
    Pie
    Posts: 22085
    88
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Another small remark, that is not yet mentioned. In the second row, the second array element, the second element on that array, is a box with nothing (and no arrows coming out of it). That box should be marked as null.

    Henry
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry Wong wrote:Another small remark, that is not yet mentioned. In the second row, the second array element, the second element on that array, is a box with nothing (and no arrows coming out of it). That box should be marked as null.

    I didn't mention because I assumed an empty box represents null To have a clear, unambiguous drawing it would indeed be better to mark empty boxes as null. Making it obvious for everyone without having to make any assumptions at all.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic