Home > Java > javaTutorial > body text

Detailed explanation of method examples for exchanging two data in java

高洛峰
Release: 2017-01-11 15:49:51
Original
1263 people have browsed it

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;
 }
Copy after login

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;
 }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template