1

I have the following code:

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.

}

What is the most efficient way to achieve this?

1 Answer 1

3

You can try using the Java Collections toArray() method:

BigInteger[] output = result.toArray(new BigInteger[result.size()]);
Sign up to request clarification or add additional context in comments.

1 Comment

I had actually tried something similiar, but I forgot to add the () after .size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.