寫 C 語言時,我們習慣用陣列逐位存放大數;而在 Java 裡,這份工作大多交給了 ArrayList。這個類提供了 clear()、equals()、add() 等三十多個方法,讓動態扩容、插入、刪除都變得輕鬆,各種形式的構造也能信手拈來。
下面以一個 VeryLongInt 類為例,示範如何用 ArrayList 存放任意精度的正整數,並完成加法與乘法運算。
[java]
import java.util.ArrayList;
import java.util.Collections;
class VeryLongInt {
ArrayList 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); // 高低位反轉回來
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());
}
}
11114532632462523462362547457357 3704845646029468841285610955774783288521573545337081737305179
ArrayList 則不強制,它提供了三種構造方法,讓你自行決定要不要指定。add() 會自動把元素接在串列末尾。size() 回傳的是目前元素個數;陣列的 length 欄位記錄的則是可容納元素的上限。set() 可以改寫指定索引的元素,效果等同陣列的直接賦值。remove() 刪除指定索引的元素,時間複雜度為 O(1);陣列要達到同樣效果,得搬移刪除點之後的所有元素。ArrayList 只要一個 indexOf() 就能搞定。a[i] = a[j+1];String[] a = {"a","b","c"};ArrayList 只能存在索引 0、1、…、size() 這些位置。int 這類原始型別)的陣列;ArrayList 的元素必須是 Object 或其子類。