时间:2010-07-26 01:49:49

标签: java arrays arraylist

5 个答案:

答案 0 :(得分:27)

答案 1 :(得分:6)

答案 2 :(得分:3)

答案 3 :(得分:1)

答案 4 :(得分:0)

使用辅助函数或两个函数很容易实现:

import java.util.*;
import java.math.BigInteger;

class Example {
    public static void main(String[] args) {
        ArrayList<BigInteger> array = newBigIntList(
            1, 2, 3, 4, 5,
            0xF,
            "1039842034890394",
            6L,
            new BigInteger("ffff", 16)
        );

        // [1, 2, 3, 4, 5, 15, 1039842034890394, 6, 65535]
        System.out.println(array);
    }

    public static void fillBigIntList(List<BigInteger> list, Object... numbers) {
        for (Object n : numbers) {
            if (n instanceof BigInteger) list.add((BigInteger)n);
            else if (n instanceof String) list.add(new BigInteger((String)n));
            else if (n instanceof Long || n instanceof Integer)
                list.add(BigInteger.valueOf(((Number)n).longValue()));
            else throw new IllegalArgumentException();
        }
    }

    public static ArrayList<BigInteger> newBigIntList(Object... numbers) {
        ArrayList<BigInteger> list = new ArrayList<>(numbers.length);
        fillBigIntList(list, numbers);
        return list;
    }
}