【问题标题】:JSON to C# : convert an arraylist of doubles to an array of ints?JSON to C#:将双精度数组列表转换为整数数组?
【发布时间】:2011-01-07 10:02:02
【问题描述】:

我有一个 JSON 库返回的双精度数组列表。在 JSON 解析器的 decode 方法运行后,我们在 C# locals 窗口中有这个:

名称 值 类型 myObj Count=4 对象 {System.Collections.ArrayList} [0] 100.0 对象 {double} [1] 244.0 对象 {double} [2] 123.0 对象 {double} [3] 999.0 对象 {double}

我的目标是从这个 ArrayList 中生成一个整数数组。一次迭代并执行一个值会很简单,但我想知道如何使用内置转换器功能来完成它。我一直在阅读 ConvertAll 上的主题,但我无法让它工作。

我无法控制 JSON 库,所以我必须从 ArrayList 开始。

谢谢

【问题讨论】:

    标签: c# arraylist double int


    【解决方案1】:

    因为拳击,你需要小心ArrayLists。因此:

    // list is ArrayList      
    int[] array = Array.ConvertAll(list.ToArray(), o => (int)(double)o);
    

    请注意,演员表的框架为(int)(double)。这首先将已装箱的 double 拆箱,然后转换为 int

    在旧版本的 .NET 中执行此操作

    // list is ArrayList
    int[] array = Array.ConvertAll(
        list.ToArray(),
        delegate(object o) { return (int)(double)o; }
    );
    

    另一种选择是

    // list is ArrayList
    int[] array = Array.ConvertAll(
        (double[])list.ToArray(typeof(double)),
        o => (int)o
    );
    

    这里我们不需要拆箱操作,因为我们首先将ArrayList 转换为一个拆箱doubles 的数组。

    在旧版本的 .NET 中执行此操作

    // list is ArrayList
    int[] array = Array.ConvertAll(
        (double[])list.ToArray(typeof(double)),
        delegate(double o) { return (int)o; }
    );
    

    【讨论】:

    • 杰森感谢拳击注意事项。我对 C# 很陌生。什么是“=>”运算符? VS2005 将其拒绝为“无效的表达式术语”。
    • => 是 lambda 运算符,o => (int)(double)o 是 lambda 表达式的示例。它基本上是一个非常特殊的匿名代表。这是 C# 3.5 中添加的一项功能。我将编辑我的帖子,向您展示如何在 Visual Studio 2005 中执行上述操作。
    • 再次感谢 Jason 的解释,并感谢其他所有回复的人,他们展示了进行这种转换的各种方法。
    【解决方案2】:

    linq 选项更简洁,但如果您没有 linq。

    //Assuming someValues is your input array and you're sure you don't need to check the types
    int[] outputValues = new int[someValues.Count];
    for (int i = 0; i < someValues.Count; i++)
       outputValues[i] = (int)someValues[i];
    

    【讨论】:

      【解决方案3】:

      如果您想要使用 ConvertAll 的工作解决方案示例,这里有一个快速的 sn-p。

       public static void testCOnvertAll()
              {
                  List<double> target = new List<double>();
                  target.Add(2.3);
                  target.Add(2.4);
                  target.Add(3.2);
      
                  List<int> result = target.ConvertAll<int>(new Converter<double, int>(DoubleToInt));
      
              }
      
              public static int DoubleToInt(double toConvert)
              {
                  return Convert.ToInt32(toConvert);
              }
      

      【讨论】:

        【解决方案4】:

        我会这样想(带转换器):

            private void Main()
            {
                List<Double> lstd = new List<Double>();
        
                lstd.Add(100.0);
                lstd.Add(244.0);
                lstd.Add(123.0);
                lstd.Add(999.0);
        
                List<int> lsti = lstd.ConvertAll(new Converter<double, int>(DoubleToInt));
        
            }
        
        
            public static int DoubleToInt(double dbl)
            {
                return (int)dbl;
            }
        

        【讨论】:

          【解决方案5】:

          林克:

          var array = (from double d in list
                       select (int)d).ToArray();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-23
            • 2014-04-18
            • 1970-01-01
            相关资源
            最近更新 更多