How to get the separate digits of an int number?

前端 未结 30 1917
陌清茗
陌清茗 2020-11-22 03:03

I have numbers like 1100, 1002, 1022 etc. I would like to have the individual digits, for example for the first number 1100 I want to have 1, 1, 0, 0.

How can I get

30条回答
  •  隐瞒了意图╮
    2020-11-22 03:27

    Just to build on the subject, here's how to confirm that the number is a palindromic integer in Java:

    public static boolean isPalindrome(int input) {
    List intArr = new ArrayList();
    int procInt = input;
    
    int i = 0;
    while(procInt > 0) {
        intArr.add(procInt%10);
        procInt = procInt/10;
        i++;
    }
    
    int y = 0;
    int tmp = 0;
    int count = 0;
    for(int j:intArr) {
        if(j == 0 && count == 0) {
        break;
        }
    
        tmp = j + (tmp*10);
        count++;
    }
    
    if(input != tmp)
        return false;
    
    return true;
    }
    

    I'm sure I can simplify this algo further. Yet, this is where I am. And it has worked under all of my test cases.

    I hope this helps someone.

提交回复
热议问题