KIng Rayhan KIng Rayhan - 8 months ago 37
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;