split int value into separate digits

后端 未结 8 1888
野的像风
野的像风 2020-11-27 18:43

I want to split my int value into digits. eg if the no. is 542, the result should be 5,4,2.

I have 2 options. 1) Convert int into String & then by using getCharA

8条回答
  •  甜味超标
    2020-11-27 19:23

    Use the mod 10 rule...

     List digits = new ArrayList();
     while (n > 0) {
         digits.add(n%10);
         n/=10;
     }
    

提交回复
热议问题