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

how to decode 3D array declaration and assignment

 
Douglas Cyporyn
Greenhorn
Posts: 21
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I came across this question on an Enthuware mock exam. My eyes cannot make sense of the array positions. Even after re-writing the thing and trying different ways of formatting the code I cannot keep track of what value is in which position.

What will be the result of attempting to run the following program?



Any advice for breaking this down?
 
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
What do you think about this approach?
Pro: easy to see which combination of indexes is which element
Con: time consuming


Hope it helps!
 
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
Another possible approach.
Pro: less time consuming
Con: harder to see which index combination refers to which element



Hope it helps!
 
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
And a third one which is combination of the other 2.



Hope it helps!
 
Douglas Cyporyn
Greenhorn
Posts: 21
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This does help a lot. I had started down the path of your third option but I was not labeling the elements correctly.

Perhaps with more practice this will come faster but I have to say it a cruel question for a timed exam

Thanks so much for the help!

Doug
 
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
Douglas Cyporyn wrote:Perhaps with more practice this will come faster but I have to say it a cruel question for a timed exam

I always fear the nested for loops with a few index counters
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

To tell the truth, it is not a complicated question. )) I would advise you to pay attention to how array access expressions and, for example, methods invocation expressions are evaluated. I encountered the following samples:

1.


2.


3.


4. This is my own.
 
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
Sergei Zhylinski wrote:4. This is my own.

Would be nice if you explained why (besides the NullPointerException) i = 3, z = 3 is printed...
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
15.26.1. Simple Assignment Operator =
this is expalnation:

If the left-hand operand is an array access expression (§15.13), possibly enclosed in one or more pairs of parentheses, then:
First, the array reference subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the index subexpression (of the left-hand operand array access expression) and the right-hand operand are not evaluated and no assignment occurs.
Otherwise, the index subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and the right-hand operand is not evaluated and no assignment occurs.
Otherwise, the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
Otherwise, if the value of the array reference subexpression is null, then no assignment occurs and a NullPointerException is thrown.
Otherwise, the value of the array reference subexpression indeed refers to an array. If the value of the index subexpression is less than zero, or greater than or equal to the length of the array, then no assignment occurs and an ArrayIndexOutOfBoundsException is thrown.
Otherwise, the value of the index subexpression is used to select a component of the array referred to by the value of the array reference subexpression.
This component is a variable; call its type SC. Also, let TC be the type of the left-hand operand of the assignment operator as determined at compile time. Then there are two possibilities:
If TC is a primitive type, then SC is necessarily the same as TC.
The value of the right-hand operand is converted to the type of the selected array component, is subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the array component.
If TC is a reference type, then SC may not be the same as TC, but rather a type that extends or implements TC.
Let RC be the class of the object referred to by the value of the right-hand operand at run time.
A Java compiler may be able to prove at compile time that the array component will be of type TC exactly (for example, TC might be final). But if a Java compiler cannot prove at compile time that the array component will be of type TC exactly, then a check must be performed at run time to ensure that the class RC is assignment compatible (§5.2) with the actual type SC of the array component.
This check is similar to a narrowing cast (§5.5, §15.16), except that if the check fails, an ArrayStoreException is thrown rather than a ClassCastException.
If class RC is not assignable to type SC, then no assignment occurs and an ArrayStoreException is thrown.
Otherwise, the reference value of the right-hand operand is stored into the selected array component.
Otherwise, three steps are required:
First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.
Otherwise, the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
Otherwise, the value of the right-hand operand is converted to the type of the left-hand variable, is subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. That is right. But I would like to post a comment for the original poster and for future readers, whom this may be useful, anyway. Assignment operator has right to left associativity. so the expression:

has two operands. The left-hand one is:

and the right-hand one is:

The left-hand expression is evaluated first. It is an array access expression. The left subexpression is evaualted, the subexpression that returns an array reference. It doesn't complete abruptly, so the index subexpression is evaluated next. It doesn't complete abruptly as well. The '1' is added to the variable 'i' and this subexpression returns an index.

The right-hand expression, 'z = i++', is evaluated next. It is quiet straitforward. The result would be 'i' = 3, 'z' = 2.

Then the assignment process returns to array access expression again. 'iArray' is now a null-reference, so the NullPointerException is thrown. But the variables 'i' and 'z' have been modified.
 
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
Sergei Zhylinski wrote:But I would like to post a comment for the original poster and for future readers, whom this may be useful, anyway.

Great and clear explanation. Have a cow!
 
Douglas Cyporyn
Greenhorn
Posts: 21
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To tell the truth, it is not a complicated question.


I do tend to agree with you Sergei. Now, that is. The funny thing about what is complicated and what is not complicated is that it is completely relative to an individual's knowledge and experience. A little time spent working with multi-dimensional arrays made all the difference for me. I looked at the question once more and my eyes picked up the value immediately.

Thanks for extending the discussion. I read through all the subsequent posts and found it useful.

Doug
 
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
Douglas Cyporyn wrote:A little time spent working with multi-dimensional arrays made all the difference for me. I looked at the question once more and my eyes picked up the value immediately.

As always: practice makes perfect!
 
kavitha vakiti
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please explain the 3rd line of below code.i.e a[ (a = b)[3] ]

 
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
Hi kavitha vakiti,

First of all, a warm welcome to CodeRanch!

kavitha vakiti wrote:Please explain the 3rd line of below code.i.e a[ (a = b)[3] ]

It's pretty well explained in this thread.

Hope it helps!
Kind regards,
Roel
 
kavitha vakiti
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic