Java method to exchange two data
1: Use arrays, that is, put the numbers to be exchanged first in the array, for example, it may be used in some array sorting.
public static void swap2(int[] arr,int a,int b){ int temp =arr[a]; arr[a] = arr[b]; arr[b] = temp; } 2: By creating an object, the values of two integers are introduced into the object, and the exchange of two integers can be realized. Of course, if you want other basic data types, you only need to change the types in A.
public static void swap(Object obj1,Object obj2){ int temp; A a =(A) obj1; A b =(A) obj2; temp = an; an = bn; bn = temp; }Thank you for reading, I hope it can help you. Thank you for your support for this site!