KIng Rayhan KIng Rayhan - 1 year ago 81
Java Question

Get an int array from an int variable in java

How can I write a method for get an int array from int variable?

Answer Source

I make an idea from above responses.

  • First , convert integer to String by integer.toString() method
  • Then , Convert that string to a char array by toCharArray() method
  • At last , Convert every char to integer by type casting

    int[] intToIntArray(int num){
      String numberAsString = Integer.toString(num);
      char [] nums = numberAsString.toCharArray();
      int [] numsArray = new int[nums.length];
      for(int i = 0 ; i < numberAsString.length() ; i++){
          numsArray[i] = Character.getNumericValue(nums[i]);
      return numsArray;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download