• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array

 
Nilesh Parikh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class lost {
public static void main (String [] args){
int [] array = new int[10];
int i=5;
array[i++]=++i+i++;
System.out.println(array[5]+" "+array[6]+" "+i);
}
}
When i tried to execute this code,i got answers to be as 14 0 8.
Could anybody answer how it could be 14 0 8.
because i think that array[i++]=++i+i++;for
int i = 5 is
array[5]=6+5=11.
array[6]=?
i=?
 
Vivek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reproducing a code help analyse the additions.
public class lost {
public static void main (String [] args){
int [] array = new int[10];
int i=5;
array[i++]=++i+i++;

/* i++ in the array subscript makes the value of i as (5+1=6) then pre subscript ++i further increments it to (6+1=7) this (7) is added to its present value which is also (7) , making a addition as 14 and this is the value of array[5]. array[6] is zero bcos of default initialisation. and value of i is (i+1; i.e. 7+1 = 8)... Hence printout is 14 0 8 */
System.out.println(array[5]+" "+array[6]+" "+i);
}
}
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alkesh,
I tell you a simple(easy) method to solve this type of situations. Just follow these rules . You will get the answer correct.
1. wherever you find the var just substitute the value and when you find ++/-- operator irrespective of post/pre increment/decrement (++/--) blindly increment/decrement the latest value of that var.. You have to start from left to right. So let's start

int i=5;
array[i++]=++i+i++;
array[5(6)] = (7)7 + 7(8);
(i) for i substitute the latest value of i
(ii) for ++ symbol, increment the latest value
(iii) for -- symbol, decrement the latest value
(iv) Finally do the calculation for all non-bracketed values
(v) Be careful when the assigned var is the same as the var
which undergone all these ++/-- operations in the statement.
In this case the assigned var takes its latest value
int i = 10;
ex. i = i++ + --i;
i = 10(11) + (10)10;
So i = 10 after exe of above stmt
*** Notice there is an error here, it should be 20. It is corrected below ***
(vi) In order to do the arithmetic, justcut-down all the bracket values

array[5] = 7 + 7; //Note here i's latest value is 8
array[5] = 14;

(vi) Since all the elements of an int[] has default value 0,array[6] is 0.
System.out.println(array[5]+" "+array[6]+" "+i);
So it prints 14 0 8

Another example

int i=10;
int j = i++ + i + --i + ++i + i;
j = 10(11) + 11 + (10)10 + (11)11 + 11;
j = 10 + 11 + 10 + 11 + 11; // i's latest value = 11 here
j = 53;

Final answer i=11 and j=53

Easy isn't
regds
maha anna



[This message has been edited by maha anna (edited May 01, 2000).]

(Pointed out error, and the fact that it is corrected below - Barry Gaunt)
(removed HTML markup)

[ May 19, 2004: Message edited by: Barry Gaunt ]
[ May 19, 2004: Message edited by: Barry Gaunt ]
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOW!!! Excellent Anna.
 
eram
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats Excellent. Thanks MA.
 
Vivian, Song
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, anna:
Nice comment! But when I run the following piece of code, the result is 20 in stead of 10.
Vivian
(v) Be careful when the assigned var is the same as the var which undergone all these ++/-- operations in the statement.
In this case the assigned var takes its latest value
int i = 10;
ex. i = i++ + --i;
i = 10(11) + (10)10;
So i = 10 after exe of above stmt

 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are correct Vivian. What happened was while I was writing that post, I wanted to insist on the foll. statement
i = i++; . This example was already preset well before in my mind . And in course of writing instead of this example and just cut and pasted some other line. I am sorry for this.
Always the arithmetic is done only on unbracketed values and the result is assigned to the left hand side. Since the left hand side var happened to be the var i itself it takes the calculated value.
But the above said easy ( ) methods works as it is.
Thank you Vivian once again.
regds
maha anna

[This message has been edited by maha anna (edited March 17, 2000).]
 
Vivian, Song
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no problem, you did a nice job anyway
Vivian
 
Thunthu Ganapathy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Explanation Maha,
Thanks,
 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent explanation Maha.....
Sure helped clear things up.....
 
Kai Li
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vivian, Song:
Hi, anna:
Nice comment! But when I run the following piece of code, the result is 20 in stead of 10.
Vivian
(v) Be careful when the assigned var is the same as the var which undergone all these ++/-- operations in the statement.
In this case the assigned var takes its latest value
int i = 10;
[b] ex. i = i++ + --i;
i = 10(11) + (10)10;
So i = 10 after exe of above stmt

[/B]

[B]
hi,I am a newer..
I try this program:
class Test {
public static void main(String args[]) {
int i = 10;
i = i++ + --i;
System.out.println(i);
}
}
The o/p is 20. why?
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please apply the same procedure I mentioned above. You will get the results.
i = i++ + --i;
i = 10(11) + (10)10
i= 10+10
i=20;
regds
maha anna
[This message has been edited by maha anna (edited January 03, 2001).]
 
Lalit Goyal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Anna. This was really great.....
Cool
 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent Maha Anna!!!
 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three cheers for Maha Anna...
No One can explain like this ...
Thank a lot!!!
Bhupendra
 
saumil shukla
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maha,
can you please explain output of the follwoing code?
public class integer
{

public integer()
{
}
public static void main(String[] args)
{

int i = 10;
System.out.println(i++ + i--); //21
System.out.println("i="+i); //i= 10
System.out.println(++i + --i); //21
System.out.println("i="+i); // i=10
System.out.println(i-- + i++); //19
System.out.println("i="+i); //i=10
System.out.println(--i + ++i); //19
System.out.println("i="+i); //i=10
System.out.println(--i + i++); //18
System.out.println("i="+i); //i=10
System.out.println(++i + i--); //22
System.out.println("i="+i); /i=10
System.out.println(i-- + ++i); //20
System.out.println("i="+i); //i=10
System.out.println(i++ + --i); //20
System.out.println("i="+i); //i=10
}
}
It is so strange that i++ , ++i , --i and i-- have unexpected results.
Thanks
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accoriding to MA's explaination, i have tried to solve it.I hope u can understand.
i=10
i++ + i-- : 10(11)+11(10)=10+11=21
The last value of i is 10.
++i + --i : (11)11+(10)10=11+10=21
The last value of i is 10.
i-- + i++ : 10(9)+9(10)=10+9=19
The last value of i is 10.
--i + ++i : (9)9+10(10)=9+10=19
The last value of i is 10.
--i + i++ : (9)9+9(10)=9+9=18
The last value of i is 10.
++i + i--) : (11)11+11(10)=11+11=22
The last value of i is 10.
i-- + ++i : 10(9)+10(10)=10+10=20
The last value of i is 10.
i++ + --i : 10(11)+(10)10=10+10=20
The last value of i is 10.
Basanti
 
Richard Teston
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Ana methodology how can we evaluate this equation:
i = 1;
i += ~i - -i * ++i + i-- % ++i * i++;
printed output --> 1
does the Ana's methodology applicable to this one?
 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
array[i++]=++i+i++;
Howdy, my question is why the i in the array will be execute at first? Anyone can explain it, thx.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The placement of the parenthesis is a little different from what a person would normally write. The reason for the difference is the fact that the JVM processes the expression from left to right. The JVM can not look at the entire expression as we can. While a human can start the evaluation of an expression from the inner most parenthesis at any point in the expression--left, right, or center--the JVM must start parsing the expression on the left and work toward the right.
The difference between the human approach and the machine approach only becomes an issue when unary operators produce side effects. That's why we humans frequently have problems with expression involving unary operators with side effects. The key to success is to make sure that the unary operators are evaluated from left to right.
Once you have evaluated the unary operators with side effects (i.e. increment and decrement) there is no need to write the parenthesis as the JVM would view the expression. After the side effects are evaluated the result of the human view and the JVM view are the same. You could then write the parenthesis in a more human readable form as follows.

[ November 07, 2002: Message edited by: Dan Chisholm ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kelvin Michael:
array[i++]=++i+i++;
Howdy, my question is why the i in the array will be execute at first? Anyone can explain it, thx.

The left hand operand of the simple assignment operator is always evaluated before the right hand operand. The JVM always works through an expression from left to right. Here's another example.

Prints: [a,0],[b,1],1,0,0,2
The array index expression is evaluated before the right hand operand of the simple assignment operator. Evaluation of the array index expression causes method m to be invoked. The input parameter is the postfix increment expression with the operand i. Since the postfix increment expression returns the original value of the operand, the value zero is passed to method m along with a String containing the character a. Method m returns the value that was passed in as an input parameter. In this case, the return value is zero. As a side effect of the postfix increment expression, the value of variable i is one after the array index expression is evaluated. The right hand operand of the simple assignment operator is the return value of method m. The input parameter to method m is the postfix increment expression with variable i as the operand. The value of variable i is now one and that is the value that is passed into method m and that is the value returned by method m and assigned to element zero of the array a. As a side effect of the postfix increment expression, the value of variable i is two after method m is invoked.
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this on Richard Teston's example.
Have I got MA's methodology correct?
Any comments please?[I used [] instead of ()]
Also I used:
-N = ~N + 1 .
Therefore:
~N=-N-1

i = 1;
i += ~i - -i * ++i + i-- % ++i * i++;
printed output --> 1

i =i+( ~i - -i * ++i + i-- % ++i * i++);

1+( -2[1]--1*2[2]+2[1]%2[2]*2[3])
1+(-2--1*2+2%2*2)
1+(-2-(-1)*2+0)
1+(-2-(-1)*2)
1+-2-(-1)*2
1-2+2
1
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anna,
Your explaination is really excellent..

thankyou soo much..

Regards,
rajani.
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me rephrase my question regarding MA's methodology.
When MA says blindly how blindly?
Am I right in assuming that we dont ignore the priority of operators?
Can we ignore the left and right associativity?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic