Home  >  Article  >  Java  >  Detailed explanation of method examples for exchanging two data in java

Detailed explanation of method examples for exchanging two data in java

高洛峰
高洛峰Original
2017-01-11 15:49:511216browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn