Two integers in Java

Source: Internet
Author: User

In the process of program development, it is common to exchange the content of two variables. In the Sorting Algorithm, there is a kind of sorting method called "Exchange sorting method ". In all sorting algorithms, it is almost necessary to exchange two elements in the set to be sorted. Exchange the content of two elements in Java. If you are a novice programmer, you may encounter unexpected problems.

As we all know, java and C ++ cannot exchange two integers by passing values.

That is, the following functions cannot be successfully exchanged with two integers,


Public void swap1 (int a, int B) {// two integers cannot be exchanged when passing the value Parameter
Int t;
T =;
A = B;
B = t;
 
 
}

 

In C ++, two integers can be exchanged through reference or pointer. In essence, two integers can be exchanged through address transmission.

Void swap2 (int & a, int & B) // pass the reference
{
Int temp;
Temp =;
A = B;
B = temp;
}

 

You can also use pointers to exchange two integers.

Void swap2 (int * a, int * B) // pointer, Address Transfer
{
Int temp;
Temp = *;
* A = * B;
* B = temp;
}

 

In java, how does one implement the exchange of two integers?

 

Method 1:

Exchange by array:

If you must use a method, the following form may be:

Void swap (int [] ){
If (a = null | a. length! = 2)
Throw new IllegalArgumentException ();
Int temp = a [0];
A [0] = a [1];
A [1] = temp;
}


The code example is as follows:

// SwapInteger. java

[Java]
// Implement the exchange of Integers
 
 
Public class SwapInteger {
Public static void swap (int a []) {
// Two integers are exchanged when an array is passed.
Int t;
T = a [0];
A [0] = a [1];
A [1] = t;


}
Public static void main (String args []) {

Int [] a = new int [2];
A [0] = 3;
A [1] = 4;
Swap ();
System. out. println (a [0] + "/t" + a [1]);
}
 
}
 

 

 

2) Method 2:

Construct an object, TAKE a and B as the object's attributes, operate on the object, and finally obtain the corresponding attributes.

 

 

Some people say it can be implemented using the Integer class, which is wrong.

The reasons are as follows:

Integer does not work,
1. Integer itself is a value object and cannot be modified. (which method can be used to modify its content ?). In fact, the String object cannot be changed;
2. Even if the Integer itself can be modified, automatic packing and unpacking will not work:
Void exchange (Integer ao, Integer bo) {exchange actual data in ao and bo}
Int a, B;

Exchange (a, B); // The automatic packing mechanism generates two temporary objects. However, a and B cannot be returned when a is called.
You can only do this at most:
Integer ao =;
Integer bo = B;
Exchange (ao, bo );
A = ao;
B = bo;

 

Example: swap two numbers in a JAVA Array

This Code provides the following functions:


1. Input 10 Integers to the user and store them in Array


2. Swap the maximum and minimum values in Array


The java program is as follows:

// SwapNumber. java

[Java]
Import java. util. collections;
Public class SwapNumber {
Public static void main (String [] ar ){

Wrote input = new partition (System. in );

Int maxIndex = 0; // mark the maximum Index
Int minIndex = 0; // indicates the minimum index.
Int numbers [] = new int [10]; // declare the array to accept user input
System. out. println ("Please input ten digits :");
// Receive messages cyclically
For (int I = 0; I <numbers. length; I ++ ){
Numbers [I] = input. nextInt ();
}
Int temp = 0; // Temporary Variable
Int max = numbers [0]; // mark the maximum value
Int min = numbers [0]; // mark the minimum value
// Maximum and minimum Indexes
For (int I = 1; I <numbers. length; I ++ ){
If (numbers [I]> max ){
Max = numbers [I]; // You must assign this value to max !!!!!
MaxIndex = I;
}
If (numbers [I] <min ){
Min = numbers [I];
MinIndex = I;
}
}
// Output the effect after sorting
For (int a: numbers ){
System. out. print (a + "/t ");
}
// Exchange
Temp = numbers [maxIndex];
Numbers [maxIndex] = numbers [minIndex];
Numbers [minIndex] = temp;

// Output the effect after sorting
System. out. println ("sorted, output :");
For (int I = 0; I <numbers. length; I ++ ){
System. out. print (numbers [I] + "/t ");
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.