c# - 整数到整数数组 C#

标签 c# arrays int

<分区>

我不得不将它的每个值拆分为一个 int“123456”到一个 Int[],我已经有了一个解决方案,但我不知道是否有更好的方法: 我的解决方案是:

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;
}

最佳答案

使用 LINQ 的简单解决方案

int[] result = yourInt.ToString().Select(o=> Convert.ToInt32(o) - 48 ).ToArray()

关于c# - 整数到整数数组 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4580261/

相关文章:

javascript - JQuery/Javascript - 下拉列表从另一个下拉列表获取信息

javascript - 我得到 [1,15],而不是 [1,0,15]。 JavaScript 过滤器

Python - 为什么 `int(x, 2)` 只能用于字符串,不能用于整数?

c++ - 为什么在 C 和 C++ 中算术运算之前必须将 Short 转换为 int?

c# - C# 应该有一个 forone 结构吗? (或者处理零个或一个案例的最干净的方法是什么)

c# - 尝试将 List<Point> 的内容写入 txt 文件(Windows 窗体 C#)

php - 为什么 carbon addMinutes 加法不好

python - Python 中是否有内置函数可以更改整数的基数?

c# - 我如何使用 Elysium 项目?

c# - 按位置访问泛型类型的属性