Walnuts, posterity
Contents of this lecture: about two numbers exchanged
for In Java, for a basic data type, Java passes a copy copy and cannot change the value of the parameter at all. You can do this through an array, because the array passes the address.
Example one:
public class Text {public static void Main (string[] args) { int a=4; int b=5; Change (A, b); System.out.println ("main function:" +a+ "," +b); } public static void Change (int a, int b) { int temp; temp = A; A = b; b = temp; SYSTEM.OUT.PRINTLN ("Swapped data:" + A + "," + b); } }
Output:
Data exchanged: 5, 4 main function: 4,5
Example two:
public static void Change (int a, int b) { //In this implementation, only the two variables themselves are different//or implemented. A = a ^ b; b = a ^ b; A = a ^ b; SYSTEM.OUT.PRINTLN ("Swapped data:" + A + "," + b); }
Example three:
public static void Change (int a, int b) { //In this implementation, only the two variables themselves are added minus the implementation. A = a + B; b = a A; A = a-B; SYSTEM.OUT.PRINTLN ("Swapped data:" + A + "," + b); }
Example four:
public class Text {public static void Main (string[] args) { int[] a = new int[2]; A[0] =4; A[1] =5; Change (A, 0, 1); System.out.println ("main function:" +a[0]+ "," +a[1]); } public static void Change (int[] A, int x, int y) { int temp = a[x]; A[X] = A[y]; A[y] = temp; System.out.println ("Post-Exchange data:"); System.out.println ("x=" + a[0]); System.out.println ("y=" + a[1]); } }
Output:
Data exchanged: x=5y=4 main function: 5,4
This is where we go, take your time and enjoy it
On the issue of exchanging two numbers