Integer to Integer Array C#

前端 未结 14 1585
深忆病人
深忆病人 2020-12-08 04:42

I had to split an int \"123456\" each value of it to an Int[] and i have already a Solution but i dont know is there any better way : My solution was :

publi         


        
14条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 05:27

    You can do that without converting it to a string and back:

    public static int[] intToArray(int num) {
      List numbers = new List();
      do {
        numbers.Insert(0, num % 10);
        num /= 10;
      } while (num > 0);
      return numbers.ToArray();
    }
    

    It only works for positive values, of course, but your original code also have that limitation.

提交回复
热议问题