Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在两个列表/数组上正确执行算术运算_C#_Arrays_List_Linq - Fatal编程技术网

C# 如何在两个列表/数组上正确执行算术运算

C# 如何在两个列表/数组上正确执行算术运算,c#,arrays,list,linq,C#,Arrays,List,Linq,我有2个数组需要对其执行算术运算(+、-、/、*) 编辑 上面显示的空元素仅用于表示数据 它们都存储在没有空格的地方 Array1 = d1, d2, d3, d4, d5, d6, d9, d10 Array2 = d3, d4, d6, d8, d9, d10 请求输出 ArrayOpt = [0] = d3+d3 [1] = d4+d4; [2] = d5+d4; [3] = d6+d6; [4] = d6+d8; [5] = d9+d9; [6] = d10+d10;

我有
2个数组
需要对其执行算术运算(+、-、/、*)

编辑 上面显示的空元素仅用于表示数据 它们都存储在没有空格的地方

Array1 = d1, d2, d3, d4, d5, d6, d9, d10
Array2 = d3, d4, d6, d8, d9, d10
请求输出

ArrayOpt = 

[0] = d3+d3

[1] = d4+d4;

[2] = d5+d4;

[3] = d6+d6;

[4] = d6+d8;

[5] = d9+d9;

[6] = d10+d10;
我尝试过的

  int i = 0, PreviousIndex = -1;
   int SecondaryIndex = 0;

 // check if there is a gap
if (SecondaryIndex < DATES2.Length && Convert.ToInt32(d) != Convert.ToInt32(DATES2[SecondaryIndex]))
{
        // first value of index contain the index of second symbol date that matches with first symbol's first date.
        // eg: first data     => d1 d2 d3 d4 d5 d6
        //      2nd data      =>       d3 d4    d6
        // in the above case, the index would be the index of d3.
        index = DATES2.Select((v, j) => new { Index = j, Value = (int)v }).Where(p => p.Value == (int)d).Select(p => p.Index).ToList();
        if (index.Count > 0)
             SecondaryIndex = index[0];
        else
             SecondaryIndex = -1;
}

if(secondaryIndex != -1)
{
    CalculateData(operation, DATES1[i],DATES2[secondaryIndex]);
    PreviousIndex = secondaryIndex;
}
else
{
   CalculateData(operation, DATES1[i],DATES2[PreviousIndex]);
}

i++;
secondaryIndex++;
有人能提出什么问题或其他更好的解决方案吗?

您可以使用库来执行基本的线性代数运算和所有您可能想要使用的矩阵运算。正如您可以在链接中看到的那样,基本线性代数运算示例显示(+、*、-、/)

另外,由于矩阵是一维的,所以可以使用单个for循环对Array1和Array2索引求和,如下所示

var a1 = new int[5] {1,2,3,4,5}; 
var a2 = new int[7] {1,2,3,4,5,6,7};

var maxLength = a1.Length > a2.Length ? a1.Length : a2.Length;

var outputArray = new int[maxLength];

for(var i = 0; i < maxLength; i++)
{
    if(a1.Length < i + 1)
    {
        outputArray[i] = a2[i];
        continue;
    }

    if(a2.Length < i + 1)
    {
        outputArray[i] = a1[i];
        continue;
    }

    outputArray[i] = a1[i] + a2[i];
}

Console.Write(outputArray.GetValue(6));
vara1=newint[5]{1,2,3,4,5};
var a2=新的整数[7]{1,2,3,4,5,6,7};
var maxLength=a1.长度>a2.长度?a1.长度:a2.长度;
var outputArray=新整数[maxLength];
对于(变量i=0;i
那么如何在双精度数组中表示空值或不存在的值呢?例如,第二个数组中不存在d1、d2,如何将它们声明为代码?或者换句话说,你如何识别d3实际上是d3,现在缺失的d1,d2?d3是一个日期,比如说“2018年12月25日”d2和d1分别是12月23日和24日。第二个列表的第一个元素是“2018年12月25日”。你的例子很难理解。例如,
[4]=d6+d8
没有什么意义。确保你写的是正确的。从我可以看出,忽略异常情况,您的第二个数组是值和“洞”的混合体。洞可能在开始,结束,或在中间。由于中间孔是可能的,所以不能只使用开始和停止范围。相反,您需要表示每个孔。我要做的是使第二个数组成为一个
double?[]
,并用
null
s表示孔。然后,只需在
for
循环中循环数组。这里的组合
d6+d8
是有意义的,因为这里有一个最后使用的日期,并且不适用于22(在第一个列表中)和25(在第二个列表中),因为第二个产品在25之前从未存在过,但在d8(在第二个列表中)d6(在第一个列表中)作为上次使用的日期存在@自从我开始工作以来,关于日期的评论就出现了。如果这是您的情况,请使用两个
字典
。然后使用for循环(每次迭代增加一天),并使用当天,在两个字典中查找对应的两个,如果启用或两者都存在,则执行适当的操作。
d1, d2, d3, d4, d5, d6, d9, d10
+
d3, d4, d6, d8, d9, d10
var a1 = new int[5] {1,2,3,4,5}; 
var a2 = new int[7] {1,2,3,4,5,6,7};

var maxLength = a1.Length > a2.Length ? a1.Length : a2.Length;

var outputArray = new int[maxLength];

for(var i = 0; i < maxLength; i++)
{
    if(a1.Length < i + 1)
    {
        outputArray[i] = a2[i];
        continue;
    }

    if(a2.Length < i + 1)
    {
        outputArray[i] = a1[i];
        continue;
    }

    outputArray[i] = a1[i] + a2[i];
}

Console.Write(outputArray.GetValue(6));