• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot understand the program

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Twisty {
{ index = 1; }
int index;
public static void main(String[] args) {
new Twisty().go();
}
void go() {
int [][] dd = {{9,8,7}, {6,5,4}, {3,2,1,0}};
System.out.println(dd[index++][index++]);
}
}


Please help me to understand this program, i am unable to understand as to how the output shows as 4 without any errors on running the program.

Regards,

Sid.
 
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sid noob wrote:class Twisty {
{ index = 1; }
int index;
public static void main(String[] args) {
new Twisty().go();
}
void go() {
int [][] dd = {{9,8,7}, {6,5,4}, {3,2,1,0}};
System.out.println(dd[index++][index++]);
}
}


Please help me to understand this program, i am unable to understand as to how the output shows as 4 without any errors on running the program.

Regards,

Sid.



Here are the explanations:
You have a class called Twisty. This class has one instance variable "index" which is initialized to ZERO. You start with the "main" method, which creates an instance (or an object) of type "Twisty" at which point the "initialization block ( { index =1 } ) assigns the value of ONE to index. Right after we are done with the object creation and initialization, we invoke method go();

Method "go()" creates a two-dimensional array (array of arrays) and initializes with the following:{{9,8,7}, {6,5,4}, {3,2,1,0}}
The first sequence of integers (9,8,7) would be index 0, the second one (6,5,4) would be at index 1 and so on.
So, consider you want to get the integer "7" from the first array (9,8,7). You would do that by getting the element at index [0][2]. The ZERO obtains the whole array at index 0 which is {9,8,7}. The TWO obtains the third element from that same array, hence we get integer of value 7.

The System.out.println(); code prints the element, but you have to be careful with the post-incrementing the index instance variable.

This gives you the answer of 4. Remember post-increment works as follows: use the variable first and then increment.
 
sid noob
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boris, thanks for the beautiful and lucid explanation. You actually cleared some earlier concepts too that was foggy. Great explanation, i understood multi-dimensional arrays as well as how the init block actually works and not to forget the post decrement operator [the chapter am studying].

 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sid noob wrote:Boris, thanks for the beautiful and lucid explanation. You actually cleared some earlier concepts too that was foggy. Great explanation, i understood multi-dimensional arrays as well as how the init block actually works and not to forget the post decrement operator [the chapter am studying].



No problem! Good luck!
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's important to know that the left-most brackets get evaluated first.

this produces 7 :


whereas this code produces 5 :
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out this code style and see if you can decipher

Aashu Mahajan
If you understood, try following and guess what would be the output : [Use Pencil and Paper]






See my responses for more info:
https://coderanch.com/t/539847/java-programmer-SCJP/certification/Examlab-array-initialization
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic