I had to split an int "123456" each value of it to an Int[] and i have already a Solution but i dont know is there any better way : My solution was :
public static int[] intToArray(int num){ String holder = num.ToString(); int[] numbers = new int[Holder.ToString().Length]; for(int i=0;i<numbers.length;i++){ numbers[i] = Convert.toInt32(holder.CharAt(i)); } return numbers; }
Apache Commons Lang's ArrayUtils class provides a toPrimitive() method that can convert an Integer object array to array of primitive ints. We need to convert a Set<Integer> to a primitive integer array first. We can use Set. toArray() for easy conversion.
Here are teh steps. First, get the size needed to store all the digits in the number -- do a malloc of an array. Next, take the mod of the number and then divide the number by 10. Keep doing this till you exhaust all digits in the number.
A simple solution using LINQ
int[] result = yourInt.ToString().Select(o=> Convert.ToInt32(o) - 48 ).ToArray()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With