Implementation of the SWAp () method in Java

Source: Internet
Author: User

We can see the pointer and reference, and the implementation of the classic swap () method. Almost everyone knows that to implement a correct swap () method, the pointer or reference must be used as the parameter. For example:

C code

  1. // Use Pointer
  2. VoidSwap (Int*,Int* B ){
  3. IntTMP = *;
  4. * A = * B;
  5. * B = TMP;
  6. }
  7. // Reference
  8. VoidSwap (Int&,Int& B ){
  9. IntTMP =;
  10. A = B;
  11. B = TMP;
  12. }

Suddenly, how does one implement this process in Java? Java does not have the so-called pointer and reference concepts, and all the passing in Java is value passing. That is to say, the following method is not effective in Java:

 

Java code
  1. VoidSwap (IntA,IntB ){
  2. IntTMP =;
  3. A = B;
  4. B = TMP;
  5. }

So this is quite embarrassing. To achieve a correct exchange process, it seems that more complicated things must be done.

 

If A and B are two member variables in the class, you can directly write the following method:

 

Java code
  1. IntA = 4;
  2. IntB = 5;
  3. Public VoidSwap (){
  4. IntTMP =;
  5. A = B;
  6. B = TMP;
  7. }


Because all methods and variables in Java must be declared in a class, A, B, and swap () are in the same class at this time. Therefore, no parameters need to be passed () method directly references the member variables of the object.

 

Another method is to re-assign values. For example, you can write a variable method:

 

Java code
  1. Public Static Int[] Swap (IntA1,IntA2 ){
  2. IntTMP = A1;
  3. A1 = a2;
  4. A2 = TMP;
  5. Return New Int[] {A1, a2 };
  6. }

According to the order in which the data is transmitted, the corresponding value is retrieved again. For example:

 

Java code
  1. IntA = 4;
  2. IntB = 5;
  3. Int[] Swap = swap (A, B );
  4. A = swap [0];
  5. B = swap [1];
  6. System. Out. println (a + "" + B );

It seems that there is no pointer or reference, and there is also a big problem. As a matter of principle, the following method should apply to all exchanges:

Java code
  1. Public Static<T>VoidSwap (T T1, t T2 ){
  2. T TMP = T1;
  3. T1 = t2;
  4. T2 = TMP;
  5. }

However, after testing, we will find that this method is always invalid. Therefore, we must always think of other methods to achieve this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.