how to convert int into int array of digits in java

how to convert int into int array of digits in java

To convert an int into an int array of digits in Java, you can use the toCharArray method of the String class to convert the int to a char array, and then use a loop to parse each char as an int and store it in the array.

Here is an example of how you can convert an int into an int array of digits in Java:

refer ‮t‬o:lautturi.com
public static int[] intToArray(int n) {
  char[] digits = String.valueOf(n).toCharArray();
  int[] array = new int[digits.length];
  for (int i = 0; i < digits.length; i++) {
    array[i] = Character.getNumericValue(digits[i]);
  }
  return array;
}

This code defines a intToArray method that takes an int as an argument and returns an int array containing the digits of the int. The method first converts the int to a char array using the toCharArray method of the String class. It then creates an int array with the same length as the char array, and uses a loop to parse each char as an int using the Character.getNumericValue method, and store it in the int array. Finally, the int array is returned.

Created Time:2017-11-01 12:05:14  Author:lautturi