Created
April 10, 2012 18:22
-
-
Save haakonn/2353416 to your computer and use it in GitHub Desktop.
Utility to concatenate an array of single digits ([1,2,3]) into an int (123)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayToIntUtil { | |
public static int a2i(int... a) { | |
int result = 0; | |
int f = (int) Math.pow(10, a.length); | |
for (int i : a) result += i * (f /= 10); | |
return result; | |
} | |
public static void main(String... args) { | |
System.out.println(a2i()); // prints 0 | |
System.out.println(a2i(6)); // prints 6 | |
System.out.println(a2i(6, 1, 3, 5, 5, 1)); // prints 613551 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment