Convert an integer to an array of digits
To convert an integer to an array of digits in Java, you can use the toCharArray()
method of the String
class to convert the integer to a string, and then use the toCharArray()
method to convert the string to an array of characters.
Here is an example of how to do this:
int number = 12345;
char[] digits = String.valueOf(number).toCharArray();
In this example, the String.valueOf()
method is used to convert the integer to a string, and the toCharArray()
method is used to convert the string to an array of characters. The resulting digits
array will contain the digits of the integer in the order they appear.
You can also use the String.split()
method to split the string into an array of strings, each containing a single digit:
int number = 12345;
String[] digits = String.valueOf(number).split("");
In this example, the split()
method is called with an empty string as the delimiter, which splits the string into an array of single-character strings.
I hope this helps. Let me know if you have any questions.