Jump to content

Swap method

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
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?

#2
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
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
Renshi

Renshi

    Newbie

  • Members
  • Pip
  • 3 posts
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!

#4
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
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:

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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
Java have no pass by reference or pointer, you can use a wrapper:
// 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.

#6
Renshi

Renshi

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#7
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
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...