简体   繁体   English

将int值拆分为单独的数字

[英]split int value into separate digits

I want to split my int value into digits. 我想将我的int值拆分成数字。 eg if the no. 例如,如果没有。 is 542, the result should be 5,4,2. 是542,结果应该是5,4,2。

I have 2 options. 我有2个选择。 1) Convert int into String & then by using getCharArray(), i can have separate characters & then i will convert them back into int values. 1)将int转换为String然后使用getCharArray(),我可以有单独的字符然后我将它们转换回int值。

2) Convert int into String, without converting it into char array, iterate it & get all digits. 2)将int转换为String,而不将其转换为char数组,迭代它并获取所有数字。

Is there any other way to solve the problem. 有没有其他方法可以解决这个问题。 If not, which of the option will be fast? 如果没有,哪个选项会快?

List<Integer> digits(int i) {
    List<Integer> digits = new ArrayList<Integer>();
    while(i > 0) {
        digits.add(i % 10);
        i /= 10;
    }
    return digits;
}

Use the mod 10 rule... 使用mod 10规则......

 List<Integer> digits = new ArrayList<Integer>();
 while (n > 0) {
     digits.add(n%10);
     n/=10;
 }
int num = 542;

if (num<0) num=-num; // maybe you'd like to support negatives
List<Integer> digits = new LinkedList<Integer>();

while (num>0) {
    digits.add(0, num%10);
    num=num/10;
}

System.out.println(Arrays.toString(digits.toArray())); // [5, 4, 2]

除以十并得到余数,将它们放入你选择的集合/数组中,继续这样做,直到商为零,你所拥有的只是一个余数

You could use a Stack instead of an ArrayList if the ordering was a big issue. 如果排序是一个大问题,您可以使用堆栈而不是ArrayList。 When popped the digits off the stack you would get them in the correct order, with the most significant digit first. 当从堆栈中弹出数字时,您将以正确的顺序获得它们,首先是最重要的数字。

int digits(int i) {
    int num=0;
    while(i > 0) {
        num *= 10;
        num += i % 10;
        i /= 10;
    }
    return num;
} 

This will split the digits for you. 这将为您分割数字。 Now put them into an array instead of printing them out and do whatever you want with the digits. 现在将它们放入一个数组而不是打印出来,并用数字做任何你想做的事情。 If you want to add them, you could replace the System.out with something like sum += z; 如果要添加它们,可以使用sum += z;等替换System.out sum += z; .

public class Splitter {
    public static int numLength(int n) {
        int length;        
        for (length = 1; n % Math.pow(10, length) != n; length++) {}        
        return length;
    }
    public static void splitNums(double x){
        double y, z, t = x;   

        for (int p = numLength((int)x)-1; p >= 1; p--){
             y = t % Math.pow(10, (numLength((int)(t))-1));
             z = ((t - y)/Math.pow(10, p));             
             t = t - (z * Math.pow(10, p)); 

             System.out.println(Math.abs((int)(z)));
        }   
        System.out.println(Math.abs((int)(t)));          
    }
}

This algorithm will split primitive "int" into single digits. 该算法将原始“int”分割成单个数字。 It starts from the last digit up to the first one. 它从最后一个数字开始直到第一个数字。

class IntegerSplitterDemo { class IntegerSplitterDemo {

static boolean digitChoper(int num) {

    for(int i = 10; i <= Integer.MAX_VALUE; i *= 10  ) {

        //Starts from the last digit so it will display the int in reverse order
        int remainder = (i == 10) ? num % 10 : (num % i / (i /10));

        //You can store the remainder value into ArrayList
        System.out.print(remainder + " ");

        //stop iterating the loop
        if(num % i == num) { break; }       
    }
    System.out.println("");
    return true;
} 

public static void main(String[] args) {
    int[] num = {0, 371, 372, 678, 432, 569, 341, 371, 567, 2569874};
    for(int number : num) {
        digitChoper(number);
    }
} // end main

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM