• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can the value of variable i and variable j be swap safely without the destructive assigment  RSS feed

 
Zhong Yang Quan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example

int i = 1; // Assign value of 1(Memory Location) to int i
int j = 2; // Assign value of 2(Memory Location) to int j

int temp1 = i ; // Assign i variable (memory location) to int temp1
int temp2 = j ; // Assign j variable (memory location) to int temp2

i =temp2; // Replace the inital value of 1 - destructive assigment
j= temp1; // Replace the inital value of 2 - destructive assigment

Need help in this ?
 
Greg Brannon
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not familiar with the term "destructive assignment," so my answer may be off, but at most a single temp variable is needed to swap two values, and it can actually be done without any temp variables. I've posted below a programming "trick" from assembler days when swaps were done with the minimum memory impact possible. I hope it helps illustrate a possible answer to your question.



Edit: I should add that while the above uses ints and basic addition and subtraction, the method can be expanded to other variable types, including logical variables, using the appropriate operators.
 
Zhong Yang Quan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your feedback. Appericate your feedback.

Destructive Assigment ( To my knowledge ) -
the initial value of this variable is replaced by another value of another variable which is assigned to this variable in a memory location.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give an example of a "non-destructive assignment"? I just find this confusing...a variable can hold one value. So if it is holding "1", and you want to put "2" in, what choice do you have BUT to 'destroy' the "1"? Sure, you can save a copy of it off, but you are still destroying the bit pattern that held the original "1".
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My understanding of destructive assignment is that any time you reassign a value to a variable which has a value, it is a destructive assignment. This is the typicalal case in Java, but in some languages this isn't allowed, all variables are considered final. So I guess to the closest thing to enforcing the op's original question would be: Given a method signature as above, how do you swap the values of the two parameters?

I can think of no way to do it in reality, but you can emulate it by creating different namespaces to hold the values such as:
1) calling the method again with the parameters in the opposite order


2) Using something like an inner class to do the same sort of manipulation. ( on an iPad, so not really able to write code to test or show this but I would be surprised if it couldn't be done this way.)

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!