• Post Reply Bookmark Topic Watch Topic
  • New Topic

increment array and got unexpecting result  RSS feed

 
Andrew Chiu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a newbie question which makes me struggle couple hours and search for nothing. will you please kindly help me on this? i have no one to ask since i am study by my own. thank you for your time and support.


However, the result for array[0] is B. Not A. from my understanding, the array[0] should be 'A'.

can anyone welling to share the opinion? appreciate for your help.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

you should not be too surprised by this - i gets incremented, after all, before you use it in the assignment. The post-increment that takes effect after usage only applies to the "array[i++]" part; after that, the increment has been done. See http://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment for some more discussion on this.

I don't use any of the pre/post-increment operators in expressions on principle - they make code more difficult to read, IMO.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!