KIng Rayhan KIng Rayhan - 6 months ago 31
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?


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;