Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer to Integer Array C# [duplicate]

Tags:

arrays

c#

int

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; } 
like image 222
Rosmarine Popcorn Avatar asked Jan 02 '11 20:01

Rosmarine Popcorn


People also ask

Can integer convert to array?

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.

How do you store integer digits in array?

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.


1 Answers

A simple solution using LINQ

int[] result = yourInt.ToString().Select(o=> Convert.ToInt32(o) - 48 ).ToArray() 
like image 69
Jahan Zinedine Avatar answered Oct 04 '22 18:10

Jahan Zinedine