java Method to exchange two data
1: Use an array, that is, first put the numbers to be exchanged 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 to realize the exchange of two integers. Of course, if you want other basic data types, you only need to change the type in A.
public static void swap(Object obj1,Object obj2){ int temp; A a =(A) obj1; A b =(A) obj2; temp = a.n; a.n = b.n; b.n = temp; }
Thank you for reading, I hope it can help everyone, thank you for your support of this site!
For more java method examples for exchanging two data, please pay attention to the PHP Chinese website for related articles!