[java] swap

C 의 방법

  1. void swap( int& a, int& b)
  2. {
  3.    int tmp;
  4.    tmp=a;
  5.    a=b;
  6.    b=tmp;
  7. }
  8. void swap( int* a, int* b)
  9. {
  10.    int tmp;
  11.    *a=*b;
  12.    *b=tmp;
  13. }

 

Java의 방법

  1. public class SwapTest
  2. {
  3.    public static void main(String[] args)
  4.    {
  5.        Int aa = new Int(1234);
  6.        Int bb = new Int(2345);
  7.        aa.swap(bb);
  8.        System.out.println(aa.getNum());
  9.        System.out.println(bb.getNum());
  10.    }
  11. }
  12. class Int
  13. {
  14.      private int num;
  15.     Int(int num) {
  16.       this.num = num;
  17.     }
  18.     public void swap(Int n) {
  19.        Int temp = new Int(this.num);
  20.        n.setNum(temp.getNum());
  21.     }
  22.     public int getNum() {
  23.        return num;
  24.     }
  25.     public void setNum(int num) {
  26.        this.num = num;
  27.     }
  28. }

클래스의 멤버 함수로해서 값을 바꿔야 한다.

사이트: http://edwards.tistory.com/212

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s