Java - 如何将BigInteger设置为BigInteger数组

时间:2016-06-02 06:30:52

标签: java biginteger

我有以下代码:

public static BigInteger[] Cubes (int m){
    Set<BigInteger> result = new HashSet<BigInteger>();

    for (int i = 1;; i++){
        BigInteger check = BigInteger.valueOf(i).pow(3);
        if (String.valueOf(check).length() == m){
            result.add(check);
        }
        if (String.valueOf(check).length() > m) break;
    }

    // I now have a BigInteger set containing all my values. 
   // I need to cast this to a BigInteger[] over here.
   // Then, I can return a BigInteger array.

}

实现这一目标的最有效方法是什么?

1 个答案:

答案 0 :(得分:3)

您可以尝试使用Java Collections toArray() method

BigInteger[] output = result.toArray(new BigInteger[result.size()]);