在C语言中我们经常用数组处理大数问题,在Java中数组功能逐渐被ArrayList类代替,强大的ArrayList类提供了clear(),equals()等32个方法,所以我们能轻松实现各种类的构造,以下代码将提供VeryLongInt类的设计:
[java]
importjava.util.ArrayList;
importjava.util.Collections;
classVeryLongInt{
ArrayList digits;//digits字段,存放大数的各位数字
publicVeryLongInt(){
finalintINITIAL_CAPACITY =500;
this.digits =newArrayList(INITIAL_CAPACITY);
};//无参构造
publicVeryLongInt(String s){
this.digits =newArrayList(s.length());
for(inti =0; i
charc=s.charAt(i);
if(c>='0'&& c<='9'){
this.digits.add(newInteger(c -'0'));
}
}
};//含参构造
publicString toString(){
StringBuffer s =newStringBuffer("");
for(inti=0;i
s.append(this.digits.get(i));
returns.toString();
}//将大数转为字符串形式
publicvoidadd(VeryLongInt otherVeryLong){
intlargerSize,partialSum,carry=0;
VeryLongInt sum =newVeryLongInt();
largerSize =this.digits.size()>otherVeryLong.digits.size() ?this.digits.size():otherVeryLong.digits.size();
for(inti=0;i
partialSum =this.least(i)+otherVeryLong.least(i)+carry;
carry = partialSum /10;
sum.digits.add(newInteger(partialSum%10));
}
if(carry ==1)
sum.digits.add(1);
Collections.reverse(sum.digits);//反转sum
this.digits.clear();
this.digits = sum.digits;
}//大数加法
publicvoidmult(VeryLongInt otherVeryLong){
inti =1;
if(((Integer)otherVeryLong.digits.get(0)).intValue()==0){
this.digits.clear();
this.digits.add(0);
return;
}
VeryLongInt sum =newVeryLongInt("1");
VeryLongInt mult =newVeryLongInt(this.toString());
while(!sum.equal(otherVeryLong)){
this.add(mult);
sum.add(newVeryLongInt("1"));
}
}//任意大数与单个数字相乘
publicvoidmultiply(VeryLongInt otherVeryLong){
intlength = otherVeryLong.digits.size();
VeryLongInt multiply;
VeryLongInt multiplycopy =newVeryLongInt(this.toString());
this.digits.clear();
this.digits.add(0);
for(inti=0;i
multiply =newVeryLongInt(multiplycopy.toString());
multiply.mult(newVeryLongInt((otherVeryLong.digits.get(i)).toString()));
for(intj=1;j
multiply.digits.add(0);
this.add(multiply);
}
}//大数与大数相乘
publicintleast(inti ){
returni>=this.digits.size()?0:((Integer)this.digits.get(this.digits.size()-i-1)).intValue();
}//返回从低位到高位第i位数字
publicbooleanequal(VeryLongInt v1){
if(this.digits.size()!=v1.digits.size())
returnfalse;
intlength =this.digits.size();
for(inti=length-1;i>=0;i--){
if(((Integer)this.digits.get(i)).intValue()!=((Integer)v1.digits.get(i)).intValue())
returnfalse;
}
returntrue;
}//判断两大数是否相等
}
publicclassArrListDemo{
publicstaticvoidmain(String args[]){
VeryLongInt str1 =newVeryLongInt("11114532632462523462362547457357");
str1.add(newVeryLongInt("0"));
System.out.println(str1.toString());
str1.multiply(newVeryLongInt("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或其子类。