2016-12-19 12 views
0

私はC#を使用しています。 2次元配列の値を別の2次元配列に置きたい。2次元配列値が配列に置き換えられます

値を置き換えるために何をしたいアレイは

{ 
    {100, 100, 100, 100}, 
    {100, 100, 100, 100}, 
    {100, 100, 100, 100}, 
    {100, 100, 100, 100} 
} 

と、その配列に値を置き換えるために、どのようないくつかの配列が

{ 
    {500,500}, 
    {500,500} 
} 

であると私は予想:この中

{ 
    {100,100,100,100}, 
    {100,500,500,100}, 
    {100,500,500,100}, 
    {100,100,100,100} 
} 
+0

あなたが援助のいずれかの希望をしたい場合は、あなたのコードをポストする必要があります。 – Daniel

+0

とその背後にある論理は何ですか? – mybirthname

答えて

0

二つのループでコードを理解するシンプル:あなたはあなたの要件に応じて、このコードをアップグレードする必要があります。もちろん、

public static void ReplaceValues (int[,] destinationArray, int[,] replaceWith, int columnOffset, int rowOffset) 
{ 
    for (int row = 0; row < replaceWith.GetLength (0); row++) 
    { 
     for (int column = 0; column < replaceWith.GetLength (1); column++) 
     { 
      destinationArray[row + rowOffset, column + columnOffset] = replaceWith[row, column]; 
     } 
    } 
} 

var bigger = new int[,] 
{ 
    {100, 101, 102, 103, 104}, 
    {100, 100, 100, 100, 100}, 
    {100, 100, 100, 100, 100}, 
    {100, 100, 100, 100, 100}, 
}; 

var smaller = new int[,] 
{ 
    {1, 2}, 
    {3, 4}, 
}; 

ReplaceValues (bigger, smaller, 3, 2); 

と静的メソッド。

結果:

Result

0

場合は、これらの配列の次元を比較する必要があります: 大きな配列です:4x4とth eは2x2(double)です。だから、大きな配列をループがある:

bigarray[i x 2 + 1, j x 2 + 1] = smallarray [i,j]. 

のでformularができ:

bigarray[i x compare_value + 1, j x compare_value + 1] = smallarray [i,j] 

compare_value = bigarray/ smallarayで。