C# 求和、减法和乘法数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13928327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:07:29  来源:igfitidea点击:

Sum, Subtract and Multiply arrays

c#

提问by Yushell

I've got the following arrays:

我有以下数组:

int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } };
int[,] myArray2 = new int[2, 3] { { 6, 4, 3 }, { 8, 2, 8 } };

What I'd like to know how to do is:

我想知道怎么做的是:

  1. Create a new array with the sum of myArray1 and myArray2
  2. Create a new array with the subtraction of myArray1 and myArray2
  3. Create a new array with the multiplication of myArray1 and myArray2
  1. 使用 myArray1 和 myArray2 的总和创建一个新数组
  2. 创建一个减去 myArray1 和 myArray2 的新数组
  3. 用 myArray1 和 myArray2 相乘创建一个新数组

Result of sum would be:

总和的结果将是:

int[,] myArray3 = new int[2, 3] { { 7, 6, 0 }, { -4, 4, 0 } };

Result of subtraction would be:

减法的结果是:

int[,] myArray3 = new int[2, 3] { { 5, 2, 6 }, { 12, 8, 16 } };

Result of multiplication would be:

乘法的结果是:

int[,] myArray3 = new int[2, 3] { { 6, 8, 9 }, { 32, 12, 64 } };

Can this be done similar to printing out the arrays, with for loops? I tried looking for examples but found none that I could use for my specific problem.

这可以类似于打印出数组,使用 for 循环来完成吗?我尝试寻找示例,但没有发现可以用于解决特定问题的示例。

采纳答案by Sayse

int[,] a3 = new int[2,3];

for(int i = 0; i < myArray1.GetLength(0); i++)
{
for(int j = 0; j < myArray1.GetLength(1); j++)
{
a3[i,j] = myArray1[i,j] + myArray2[i,j];
a3[i,j] = myArray1[i,j] - myArray2[i,j];
a3[i,j] = myArray1[i,j] * myArray2[i,j];
}
}

need to store a3 before doing a new calculation obviously

显然需要在进行新计算之前存储 a3

回答by Pr0n

Yes this would be done exactly like printing out the arrays with for loops

是的,这就像用 for 循环打印出数组一样

c# has foreach loops which would be even easier to use

c# 有 foreach 循环,这会更容易使用

Note: I get the idea this is for homework so I'm not going to give a 100% conclusive end all be all answer.

注意:我知道这是为了作业,所以我不会给出一个 100% 的结论性的答案。

 int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } };
 int[,] myArray2 = new int[2, 3] { { 6, 4, 3 }, { 8, 2, 8 } };

          foreach (int[] a1 in myArray1) 
          {
             foreach(int i in a1)
             {
                //operation here
                //you get the idea
             }        
          }

I find in c# arrays are kind of dinosaurs I very much prefer lists.

我在 c# 中发现数组是一种恐龙,我非常喜欢列表。

回答by d.moncada

If you want to use a for loop, you can iterate through the rows/columns of the multi-d array as follows:

如果要使用 for 循环,可以按如下方式遍历多维数组的行/列:

for (int i = 0; i < myArray1.GetLength(0); i++)
{
    for (int j = 0; j < myArray1.GetLength(1); j++)
    {
        // Here, you can access the array data by index, using i and j. 
        // Ex, myArray1[i, j] will give you the value of 1 in the first iteration.
    }
}

Note: When you pass a value into the Array's GetLength method, it represents the dimension of the array. See http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx

注意:当您将一个值传递给 Array 的 GetLength 方法时,它表示数组的维度。请参阅http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx

回答by Shaharyar

For Sum:

对于总和

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = myArray1[i, j] + myArray2[i, j];
            }                
        }

For Subtraction:

对于减法

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = myArray2[i, j] - myArray1[i, j];
            }                
        }

For Multiplication:

对于乘法

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = A[i, j] * B[i, j];
            }
        }