4

I want split the following biginteger into digits and put it in an array.

BigInteger = 123456789123456789123456789123456789
array[]={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,}

How can I do this?Thank you.I searched it but couldn't find a better answer.

0

2 Answers 2

5

Could do it as follows:

BigInteger value = new BigInteger("123456789123456789123456789123456789");
List<Integer> list = new ArrayList<Integer>();
BigInteger ten = new BigInteger("10");
while (!value.equals(BigInteger.ZERO))
{
    list.add(0, value.mod(ten).intValue());
    value = value.divide(ten);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just do toString and turn each character to int and add to array of ints

String biStr = bi.toString();
int[] ints = new int[biStr.length()];
for(int i=0; i<biStr.length(); i++) {
    ints[i] = Integer.parseInt(String.valueOf(biStr.charAt(i)))
}

Comments

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.