【问题标题】:how to convert one double Array to an another double Array (empty)如何将一个双精度数组转换为另一个双精度数组(空)
【发布时间】:2020-05-24 16:57:29
【问题描述】:

我有一个字符串是

35.318073; 65.741196; 57.335339; 87.358453; +0.573 ;; 42.282722; 92.314201; 64.299988; 114.331459; +0.537 ;; 89.842690; 109.230721; 111.859947; 131.27986; +0.644 ;;

我正在使用下面的代码对其进行拆分,然后将其转换为 JSON 对象,然后将其转换为双对象。

if (readByCount > 30)
                {

                    var output = (new string(buff).TrimEnd('\u0000'));
                    Console.WriteLine(output);

                    var output1 = output.Split(new[] { ";;" },StringSplitOptions.RemoveEmptyEntries).Select(s => s.Split(';')).ToArray();

                    JToken jsonParsed = JToken.FromObject(output1);

                    List<double> zcoordinate = new List<double>();
                    double zaxis = 0.0;
                    double[] MediumArray = { 0,0 };
                    double zCoordinateMin = 0.0;



                    foreach (var arrayItem in jsonParsed)
                    {
                        var innerArray = arrayItem.ToObject<double[]>();
                        MediumArray.Append<double[]>(innerArray);

                        zaxis = innerArray.ElementAt<double>(4);
                        zcoordinate.Add(zaxis);
                        foreach (var item in zcoordinate)
                        {
                            zCoordinateMin = zcoordinate.Min();
                        }
                    }


                    for (int i = 0; i < MediumArray.Length; i++)
                    {
                        if (MediumArray.ElementAt(i) == zCoordinateMin)
                        {
                            Console.WriteLine(MediumArray[0] + MediumArray[1]);
                        }


}

如您所见,由于输入字符串的格式,for-each 循环将运行三次。我想让我的 MediumArray 成为一个数组的 Array,这样我就可以运行 foreach 循环来查找 MediumArray[0] + MediumArray[1] 仅针对该数组中元素的最小值位于位置 5(索引 4 )。

我知道如果我有 mediumArray 一个数组的数组,我不能使用代码中显示的 for 循环,但我必须使用 for-each 循环,例如:

//foreach (var item in MediumArray)
                        //{


                        //    if (MediumArray.ElementAt(4) == zCoordinateMin)
                        //    {
                        //        Console.WriteLine(MediumArray[0] + MediumArray[1]);
                        //    }
                        //}

我正在尝试此代码,但由于此行而出现错误。

MediumArray.Append<double[]>(innerArray);

错误提示:

'double[]' 不包含 'Append' 的定义和最好的 扩展方法重载 'Enumerable.Append(IEnumerable, double[])' 需要“IEnumerable”类型的接收器

如何正确执行流程?

【问题讨论】:

  • MediumArray.Append(innerArray).ToArray() 或将其保留为 IEnumerable - MediumArray.Append(innerArray)
  • 它很简单,不要使用数组,使用列表或有附加的东西。
  • @pwrigshihanomoronimo MediumArray.Apeend(innerArray).ToArray() 不起作用。它给出了同样的错误。
  • @MichaelRandall。谢谢。我通过制作一个双 [] 列表来解决它。并为其上的每个循环运行。

标签: c# arrays json ienumerable


【解决方案1】:

这是我想出的一个可以完成这项工作的 Linq 查询:

var input = "35.318073;65.741196;57.335339;87.758453;+0.573;;42.282722;92.314201;64.299988;114.331459;+0.537;;89.842690;109.230721;111.859947;131.247986;+0.644;;";

var output = input
  .Split(";;")                                    // split input string into the double arrays
  .Where(array => !string.IsNullOrEmpty(array))   // ignore empty array (last one in your example)
  .Select(array => array                          
    .Split(";")                                   // split into single values
    .Select(value => double.Parse(value))         // parse them to doubles
    .ToArray())                                   // join them together as an array
  .ToArray();                                     // join the arrays together as an array

此查询返回System.Double[][]

【讨论】:

  • Split(";;") 不起作用,表示错误,无法从字符串转换为字符。不过谢谢。我用另一种方法解决了
  • @VedantGonnade 我在repl.it/languages/csharp 上测试了代码,它运行时没有任何错误
猜你喜欢
  • 2011-07-07
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多