Generally speaking, the implementation of large numbers is done through two methods: BigInteger and BigDecimal. These two methods represent immutable arbitrary precision integers and immutable signed arbitrary precision decimal numbers (floating point numbers). Mainly used in high-precision calculations. These two classes make high-precision operations in Java simple. However, this article does not introduce large-number operations in Java through the above two methods.
The main idea is: put two numbers in the String, then take out each number, put it in the array, start the calculation from the last bit, calculate the addition, and determine whether the carry is carried. The carry is the previous bit +1. If the length exceeds, copy it to the new array.
The code is as follows:
public class BigIntAdd {private int[] array;//Declare an array//The function to calculate the addition of large numbers public static String bigAdd(BigIntAdd fisrtNum, BigIntAdd secondNum) {String result = "";Boolean false = false;//The flag to determine whether the array is out of bounds int[] arrayOne;int[] arrayTwo;int[] arrayThree = null;//Put the longer string into the arrayOne array, because the calculation result should be put into the arrayOne array if (fisrtNum.instance().length >= secondNum.instance().length) {arrayOne = fisrtNum.instance();arrayTwo = secondNum.instance();} else {arrayOne = secondNum.instance();arrayTwo = fisrtNum.instance();} for (int i = 0; i < arrayTwo.length; i++) {if (arrayOne[i] + arrayTwo[i] < 10) {//When there is no carry, arrayOne[i] = arrayOne[i] + arrayTwo[i];} else if (arrayOne[i] + arrayTwo[i] >= 10) {//ArrayOne[i] = arrayOne[i] + arrayTwo[i] - 10;if ((i + 1) < arrayOne.length) {arrayOne[i + 1] = arrayOne[i + 1] + 1;//Add the next bit to 1} else {//When the length of arrayOne is not enough, copy it into arrayThree falg = true;arrayThree = new int[arrayOne.length + 1];System.arraycopy(arrayOne, 0, arrayThree, 0,arrayOne.length);arrayThree[arrayOne.length] = 1;//Assign the highest bit of arrayThree, equivalent to 1 in carry}}}}//Split all elements in arrayThree into strings if (falg) {for (int i : arrayThree) {result += i;}} else {for (int i : arrayOne) {result += i;}}//Reverse the result and return new StringBuffer(result).reverse().toString();}//Initialize the array method private int[] instance() {return array;}//Initialize the constructor public BigIntAdd(String num) {StringBuffer sb = new StringBuffer(num);String string = sb.reverse().toString();//Implement string inversion to facilitate calculation array = new int[string.length()];//Convert string into an array for (int i = 0; i < string.length(); i++) {array[i] = Integer.valueOf(string.substring(i, i + 1));}}public static void main(String [] args){String result=BigIntAdd.bigAdd(new BigIntAdd("521111111"), new BigIntAdd("40999999999"));System.out.println(result);}}result:
Summarize
The above is the entire content of this article about Java programming to implement two large numbers added code examples. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!