Skip to content
Advertisement

How do I convert an Int to an Int[] in java?

I want to convert a primitive Java integer value:

JavaScript

To an integer array:

JavaScript

Advertisement

Answer

There can be so many ways to do it. A concise way of doing it’s using Stream API as shown below:

JavaScript

Output:

JavaScript

Notes:

  1. ASCII value of '0' is 48, that of '1' is 49 and so on.
  2. Math#abs returns the absolute value of an int value
    • e.g. Math.abs(-5) = 5 and Math.abs(5) = 5
Advertisement