Integer Foundation Manipulation
I recently worked on a problem from leetcode that asked for an integer reversal. No big deal, I've done this type of problem with strings before so I can't imagine it's that difficult. Welp, it was. I didn't have some fundamental knowledge of how integers behave in Java and how to tear them apart. My mind first went to strings in javascript:
var name = "Jason"; alert(name[0]); //J
The above felt pretty logical. I could manipulate an integer the same way I do strings right? Well not exactly. You see, an integer in Java is looked at very differently than a string. Just because it has several digits does not mean it can be pulled apart using any type of array method. It is a number, a representation of a numerical value. Hmmmm, but I need to reverse it...so how do I reverse something I can't put into an array like I've done before with a string? Well, in Java, like a string, I could convert this into a Character array...but that doesn't work because it's a number, so I first need to turn it into a string? Then after that I need to turn it back into a string, and then into an integer? I'm pretty sure that will work but that sounds way too complicated. There should be an easier way here using basic mathematics. Let's try using modulus and divison:
int number = 321; ArrayList digits = new ArrayList();
OK, we have a number we need to flip and a place to store them. Why not just use a primitive array? Well, we don't technically know the size of our array since we can't execute a length function on the number. We can however check the length() of the arraylist once it's filled, but by that point we have what we need to get the job done. So how can we go, little by little, tearing this number apart? Well, we can divide it digit by digit. This number is base 10 so what happens if we divide by ten? We would get 32 (rounding). If we divided 32 by 10 we get 3. So little by little we remove the digits, but remember, we need to capture those digits too. Dividing just removes it but we never have access to that number. Is there a way we can record the last digit and save it before we remove it? Glad you asked because yes there is! Modulus! By running 321 % 10 we get 1, if we run 32 % 10 we get 2 and so on. So now we have the last digit and the ability to remove that digit from the number by straight division. By now you can probably see how to solve this problem but let's go ahead and look at it in code:
while(number > 0){ digits.add(number % 10); number = number / 10; }
I'm skipping over a few things here but this is the basic idea. Keep in mind the arraylist is adding these digits for us in a stack like order which is perfect because it automatically is reversing the number for us. After this all we need to do is build it back up!
number = 0; for(int i=0; i<digits.length(); i++) { number = number * 10 + digits.get(i); }
The above code will build up our new number for us and we are set! Now I didn't account for a few things. What if the number is negative for instance? We could have a int we hold called "sign" that is either a positive 1 or a negative 1 and multiply the return value by that at the end (which would mean we'd use an absolute value during the main logic to remove the negetive symbol. Anyway that's it for the most part. This code does not cover every edge case but does illustrate a very basic java integer manipulation method. :)












