Hey guys, as you might have guessed this is about swapping values.
let's say that I have a bunch of variables of the same type, and I want to swap values of two variables... and I need method swap(type a, type b);
with a and b which two variables should swap value... since nothing will happen to the actuall variables if I swap values of a and b because they are just copies of the variables that I use inside the method, and java if I'm not wrong doesn't use pointers as C does (in which case I couldn've sent in adresses of variables as parameters) how can I make the swap method work?
Swap method
Started by Roman Y, Aug 20 2010 07:36 AM
6 replies to this topic
#1
Posted 20 August 2010 - 07:36 AM
|
|
|
#2
Posted 20 August 2010 - 07:41 AM
I see now that I'm probably incorrect by saying that java doesn't use pointer and adresses because it uses refferences when it comes to class types but for the sake of argument let's work with primitive types.
#3
Posted 20 August 2010 - 07:46 AM
So you want to swap the value of a and b, so a takes the value of b and viceversa?
You can create a temp variable that takes the values. Let's get it with an example:
int a = 1;
int b = 2;
int tmp;
tmp = a // tmp = 1
a = b // a = 2
b = tmp // b = 1
do a System.out.println and see if you obtain the desired results.
I don't know if I centered the subject. I hope I guessed right!
You can create a temp variable that takes the values. Let's get it with an example:
int a = 1;
int b = 2;
int tmp;
tmp = a // tmp = 1
a = b // a = 2
b = tmp // b = 1
do a System.out.println and see if you obtain the desired results.
I don't know if I centered the subject. I hope I guessed right!
#4
Posted 20 August 2010 - 08:26 AM
no, unfortunatly you didnät you just wrote the swap method.. I might have been unclear.
suppose we have different variables let's say a-z are assigned int values, and for some reason those ints arn't in an array. suppose I want to write a letter that swaps two values. but I can't just write the swap method that looks like this:
because if I'd later want to swap values of g and b I can't write swap(g, b);
in c you use pointers and refferences so it looks like this:
in which case it works correctly, but as far as I know you can't really do that in java.
suppose we have different variables let's say a-z are assigned int values, and for some reason those ints arn't in an array. suppose I want to write a letter that swaps two values. but I can't just write the swap method that looks like this:
swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
because if I'd later want to swap values of g and b I can't write swap(g, b);
in c you use pointers and refferences so it looks like this:
swap(int* a, int* b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
in which case it works correctly, but as far as I know you can't really do that in java.
#5
Posted 20 August 2010 - 08:39 AM
Java have no pass by reference or pointer, you can use a wrapper:
Why you need this much to do swap, I do not know.
// MyInteger: similar to Integer, but can change value
class MyInteger {
private int x; // single data member
public MyInteger(int xIn) { x = xIn; } // constructor
public int getValue() { return x; } // retrieve value
public void insertValue(int xIn) { x = xIn;} // insert
}
public class Swapping {
// swap: pass references to objects
static void swap(MyInteger rWrap, MyInteger sWrap) {
// interchange values inside objects
int t = rWrap.getValue();
rWrap.insertValue(sWrap.getValue());
sWrap.insertValue(t);
}
public static void main(String[] args) {
int a = 23, b = 47;
System.out.println("Before. a:" + a + ", b: " + b);
MyInteger aWrap = new MyInteger(a);
MyInteger bWrap = new MyInteger(b);
swap(aWrap, bWrap);
a = aWrap.getValue();
b = bWrap.getValue();
System.out.println("After. a:" + a + ", b: " + b);
}
}
Why you need this much to do swap, I do not know.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#6
Posted 20 August 2010 - 08:46 AM
Understood. And from what I know, Java doesn't use pointers, but then you can think in an O-O style, by dynamically change the value.
I think the Java fellows have removed the pointers by implementing the Object-Oriented programming.
I think the Java fellows have removed the pointers by implementing the Object-Oriented programming.
#7
Posted 20 August 2010 - 08:59 AM
well I don't really understand why you went through so much trouble because your swap method doesn't really swap the a and b. you're creating two objects as middlemen to take values of a and b, swap them betwin those two and then assign a and b new values inside the main method... what I meant was if you can do the whole shabang inside the swap method that would itself swap a and b. OS I'm trying to work something out but Java keeps ruining it for me with obsense of controllable pointers and not supporting multiple return values etc...


Sign In
Create Account


Back to top









