In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "what are the ways to realize the exchange of two numbers". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the ways to realize the exchange of two numbers"?
Basic data type
With the help of the third variable
Package greedy; / * author: XiangLin creation time: 22:48 on 2020-9-15 File: XX.java IDE: IntelliJ IDEA * / import java.util.Random Public class Exchange1 {public static void main (String [] args) {/ * randomly generates an integer between 0 and 100 of two fixed sequences, * where 101indicates that the generated number range is: [0101) * / Random random = new Random (47); int a = random.nextInt (101); int b = random.nextInt (101) System.out.println ("before exchange: a =" + a + ", b =" + b); / * * use the third variable to realize the third number exchange * / int t = a; / t = = an a = b; / a = = b b = t / / b = = t = a System.out.println ("after exchange: a =" + a + ", b =" + b);}}
The console output is as follows:
If you have friends who don't understand, you can prepare three cups yourself, one empty cup represents the variable t, and the two cups are filled with water to represent the variables an and b, respectively, and then exchange the water in the two cups.
Without the help of the third variable
Package greedy; / * author: XiangLin creation time: 22:51 on 2020-9-15 File: cccc.java IDE: IntelliJ IDEA * / import java.util.Random Public class Exchange2 {public static void main (String [] args) {/ * randomly generates an integer between 0 and 100 of two fixed sequences, * where 101indicates that the generated number range is: [0101) * / Random random = new Random (48); int a = random.nextInt (101); int b = random.nextInt (101) System.out.println ("before exchange: a =" + a + ", b =" + b); a = a + b; / a = = a + b b = a-b; / / b = = a + b-b = = a, then b = an a = a-b / / a = = a + b-a = b, where a = = b System.out.println ("after exchange: a =" + a + ", b =" + b);}}
The console output is as follows:
Realize the interchange of two numbers by multiplication and division
Package greedy; / * author: XiangLin creation time: 22:54 on 2020-9-15 File: xxx.java IDE: IntelliJ IDEA * / import java.util.Random Public class Exchange3 {public static void main (String [] args) {/ * randomly generates an integer between 0 and 100 of two fixed sequences, * where 101indicates that the generated number range is: [0101) * / Random random = new Random (50); int a = random.nextInt (101); int b = random.nextInt (101) System.out.println ("before exchange: a =" + a + ", b =" + b); a = a * b; / / at this time a = a * b b = a / b; / b = = a * b / b = = a, then b = an a = a / b / / a = = a * b / a = b, where a = = b System.out.println ("after exchange: a =" + a + ", b =" + b);}}
Output:
Using the assignment operator
Since these two methods are added later by the author, the seventh and eighth methods are put in front.
Using assignment, addition and subtraction to realize the exchange of two numbers
Package greedy / * author: XiangLin creation time: 22:58 on 2020-9-15 File: xx.java IDE: IntelliJ IDEA * / public class Exchange7 {public static void main (String [] args) {/ * randomly generate two integers between 0 and 100 * where Math.random () will generate any number of double type between [0-1) * so 101indicates that the generated number range is in the range of [0-101) * / int a = (int) (Math.random () * 101). Int b = (int) (Math.random () * 101); System.out.println ("before exchange: a =" + a + ", b =" + b); a = b + a-(b = a); / / a = = b + a-a = = b, a = = b System.out.println ("after exchange: a =" + a + ", b =" + b);}} "
The console output is as follows:
Using assignment and addition and multiplication to realize the interchange of two numbers
Package greedy / * author: XiangLin creation time: 23:00 on 2020-9-15 File: xxx.java IDE: IntelliJ IDEA * / public class Exchange8 {public static void main (String [] args) {/ * randomly generate two integers between 0 and 100 * where Math.random () will generate any number of double type between [0-1) * so 101indicates that the generated number range is in the range of [0-101) * / int a = (int) (Math.random () * 101). Int b = (int) (Math.random () * 101); System.out.println ("before exchange: a =" + a + ", b =" + b); a = b + (b = a) * 0; / / a = = b + a * 0 = = b, a = = b System.out.println ("after exchange: a =" + a + ", b =" + b);}} "
The console output is as follows:
XOR
Before introducing the fourth method, I'd like to introduce you to the "XOR" operator (^) in Java.
XOR operators are one of the bitwise operators in Java, so what is a bitwise operator?
The bitwise operator is used to manipulate a single "bit", or binary bit, in the basic data type of an integer. As we all know, binary counting is used in computers instead of decimal counting. In other words, there is no what we call 2, 3, 4, 5 in the computer. 100... 1000... There are only zeros and ones in the computer, which enter one every two. The bitwise operator performs Boolean algebraic operations on the corresponding bits of the two parameters, that is, 0 or 1 of the two parameters represented in binary, and finally produces a result.
Of course, we rarely use bitwise operators in Java, and we first come into contact with bitwise operators, probably from C language or digital logic and circuits. In fact, the bitwise operator comes from the underlying operation of the C language, which often requires direct manipulation of the hardware to set the binary bits in the hardware register. The Java was originally designed to be embedded in the TV set-top box, so this bottom-oriented operation was retained. One of the three major versions of Java technology: the micro version of JavaME,Java platform is used for embedded development, for the development of digital set-top boxes, videophones and other electronic devices.
Now that we understand the concept of bitwise operators, let's take a look at XOR operations.
If a ^ b, if the values of an and b are different, the XOR result is 1; if the numbers of an and b are the same, the XOR result is 0.
If you want to remember, you can remember six words of mantra: the same is 0, the difference is 1.
Or friends who understand or operate can also be understood literally, if the two numbers are different (either 0 and 1, or 1 and 0), then perform or operate; if the two numbers are the same (both 0 or 1), the result is 0.
If it's still a little abstract, it doesn't matter, just go to the code:
Package greedy; / * author: XiangLin creation time: 23:05 on 2020-9-15 File: xxx.java IDE: IntelliJ IDEA * / public class Test {public static void main (String [] args) {/ * output the results in binary form * / System.out.println ("3 binary:" + Integer.toBinaryString (3)) System.out.println (binary of "4:" + Integer.toBinaryString (4)); System.out.println (binary of "3 ^ 3:" + Integer.toBinaryString (3 ^ 3)); System.out.print (binary of "3 ^ 0:" + Integer.toBinaryString (3 ^ 0)); if (3 = = (3 ^ 0)) System.out.println (", that is, decimal 3") System.out.print (binary of "4 ^ 3 ^ 3:" + Integer.toBinaryString (4 ^ 3 ^ 3)); if (4 = (4 ^ 3 ^ 3)) System.out.println (", that is, 4" in decimal system);}
Output:
The show begins with the exchange of two numbers through XOR operations.
Package greedy; / * author: XiangLin creation time: 23:08 on 2020-9-15 File: xx.java IDE: IntelliJ IDEA * / import java.util.Random Public class Exchange4 {public static void main (String [] args) {/ * randomly generates an integer between 0 and 100 of two fixed sequences, * where 101indicates that the generated number range is: [0101) * / Random random = new Random (51); int a = random.nextInt (101); int b = random.nextInt (101) System.out.println ("before exchange: a =" + a + ", b =" + b); a = a ^ b; / / at this time, a = a ^ b = a ^ b; / / b = = a ^ b ^ b = = a, then b = an a = a ^ b / / a = = a ^ b ^ a = = b, then a = = b System.out.println ("after exchange: a =" + a + ", b =" + b);}}
Output:
Reference data type
You must have learned four methods and are full of confidence in the exchange of two numbers, so let's take a look at an interview question:
Package greedy; / * author: XiangLin creation time: 23:10 on 2020-9-15 File: xx.java IDE: IntelliJ IDEA * / public class Exchange5 {public static void main (String [] args) {Integer a = 10; Integer b = 20; swop (a, b) / / print result: a = 20, b = 10 System.out.println ("a =" + a + ", b =" + b);} private static void swop (Integer a, Integer b) {/ / complete the code here}}
As shown in the code, complete the code at the specified location so that the final running result of the program is: a = 20, b = 10
You may think this is not easy, and then "brush" may be completed like the following code:
Private static void swop (Integer a, Integer b) {a = a ^ b; b = a ^ b; a = a ^ b;}
Then look at the output result and be dumbfounded:
Nani, no change? Why is that? If you want to know why, you may need to understand the Java memory model by yourself. after all, there is no C pointer in Java (whisper bb). Of course, this blogger may also publish this kind of blog in the future.
Now let me find out the correct answer!
Private static void swop (Integer a, Integer b) throws NoSuchFieldException, IllegalAccessException {/ / complete the code here / / a = a ^ b; / / b = a ^ b; int x = a; int y = b; / / use reflection to operate Integer Class c = Integer.class; Field field = c.getDeclaredField ("value") / / authorize access to private field.setAccessible (true); / / set the values of an and b to the values of y and x, respectively, field.setInt (a, y); field.setInt (b, x);}
Dangdang, the console output is as follows:
As for friends who want to know about reflection, you can follow my blog! After all, reflection is one of the three most amazing and favorite things I learned about JavaSE at that time.
As for the sixth method:
Private static void swop (Integer a, Integer b) {System.out.println ("a =" + b + ", b =" + a); / / terminate the operation of the Java virtual machine System.exit (0);} so far, I believe you have a deeper understanding of "what are the ways to achieve the exchange of two numbers", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.