Java中用ArrayList类实现正整数大数相加与相乘
Java中用ArrayList类实现正整数大数相加与相乘
在C语言中我们经常用数组处理大数问题,在java中数组功能逐渐被ArrayList类代替,强大的ArrayList类提供了clear(),equals()等32个方法,所以我们能轻松实现各种类的构造,以下代码将提供VeryLongInt类的设计:
[java]- import java.util.ArrayList;
- import java.util.Collections;
- class VeryLongInt{
- ArrayList digits; //digits字段,存放大数的各位数字
- public VeryLongInt(){
- final int INITIAL_CAPACITY = 500;
- this.digits = new ArrayList(INITIAL_CAPACITY);
- };//无参构造
- public VeryLongInt(String s){
- this.digits = new ArrayList(s.length());
- for(int i =0; i<s.length();i++){
- char c=s.charAt(i);
- if(c>='0' && c<='9'){
- this.digits.add(new Integer(c - '0'));
- }
- }
- };//含参构造
- public String toString(){
- StringBuffer s = new StringBuffer("");
- for(int i=0;i<this.digits.size();i++)
- s.append(this.digits.get(i));
- return s.toString();
- }//将大数转为字符串形式
- public void add(VeryLongInt otherVeryLong){
- int largerSize,partialSum,carry=0;
- VeryLongInt sum = new VeryLongInt();
- largerSize = this.digits.size()>otherVeryLong.digits.size() ? this.digits.size():otherVeryLong.digits.size();
- for(int i=0;i<largerSize;i++){
- partialSum = this.least(i)+otherVeryLong.least(i)+carry;
- carry = partialSum / 10;
- sum.digits.add(new Integer(partialSum%10));
- }
- if (carry == 1)
- sum.digits.add(1);
- Collections.reverse(sum.digits); //反转sum
- this.digits.clear();
- this.digits = sum.digits;
- }//大数加法
- public void mult(VeryLongInt otherVeryLong){
- int i = 1;
- if(((Integer)otherVeryLong.digits.get(0)).intValue()==0){
- this.digits.clear();
- this.digits.add(0);
- return ;
- }
- VeryLongInt sum = new VeryLongInt("1");
- VeryLongInt mult = new VeryLongInt(this.toString());
- while(!sum.equal(otherVeryLong)){
- this.add(mult);
- sum.add(new VeryLongInt("1"));
- }
- }//任意大数与单个数字相乘
- public void multiply(VeryLongInt otherVeryLong){
- int length = otherVeryLong.digits.size();
- VeryLongInt multiply;
- VeryLongInt multiplycopy = new VeryLongInt(this.toString());
- this.digits.clear();
- this.digits.add(0);
- for(int i=0;i<length;i++ ){
- multiply = new VeryLongInt(multiplycopy.toString());
- multiply.mult(new VeryLongInt((otherVeryLong.digits.get(i)).toString()));
- for(int j=1;j<length-i;j++)
- multiply.digits.add(0);
- this.add(multiply);
- }
- }//大数与大数相乘
- public int least(int i ){
- return i>=this.digits.size()?0:((Integer)this.digits.get(this.digits.size()-i-1)).intValue();
- }//返回从低位到高位第i位数字
- public boolean equal(VeryLongInt v1){
- if (this.digits.size()!=v1.digits.size())
- return false;
- int length = this.digits.size();
- for(int i=length-1;i>=0;i--){
- if (((Integer)this.digits.get(i)).intValue()!=((Integer)v1.digits.get(i)).intValue())
- return false;
- }
- return true;
- }//判断两大数是否相等
- }
- public class ArrListDemo{
- public static void main(String args[]){
- VeryLongInt str1 = new VeryLongInt("11114532632462523462362547457357");
- str1.add(new VeryLongInt("0"));
- System.out.println(str1.toString());
- str1.multiply(new VeryLongInt("333333462462346234452643634247"));
- System.out.println(str1.toString());
- }
- }
output:
11114532632462523462362547457357
3704845646029468841285610955774783288521573545337081737305179
附几点ArrayList类与数组的几点区别:
1,当构造数组时必须指定初始容量,但是ArrayList不需要,ArrayList类提供了三种构造方法,以便于你选择是否需要指定初始容量。
2,在数组中插入元素时,必须指定索引值,而在ArrayList类中的add()方法则自动选择在ArrayList对象的末尾插入元素。
3,ArrayList类中的size()方法返回ArrayList对象中元素个数,而数组中的length字段则保存了数组可以插入的元素的最大值。
4,ArrayList类中的set()方法可以更改指定索引位置的元素,类似与数组中的直接赋值。
5,ArrayList类中的remove()方法可以删除指定索引位置的元素,而且时间复杂度为O(1),而数组中实现这一功能则需要移动所删元素的后半段空间。
6,在数组中查找是否具有某一元素需要进行显式搜索,而在ArrayList类中只需一个indexOf()方法即可搞定。
当然,数组在一些地方依然有着ArrayList类无法替代的位置:
1,索引运算符可以用来访问或者修改数组中的元素。
eg:a[i]=a[j+1];
2,数组可以在构造的时候进行初始化。
eg:String []a = {"a","b","c"};
3,我们可以在数组的任意索引位置存储元素,但是对于ArrayList对象,只能在索引值为0、1、....、size()的位置存储元素。
4,可以创建任意类型(甚至是原始类型,例如int)的数组。对于ArrayList对象,每个元素的类型必须是Object或其子类。
评论暂时关闭