2016-07-25 128 views
0

我有鋸齒2D陣列像這樣:複製2D陣列從鋸齒狀2D陣列到另一個鋸齒狀2D陣列

static void Main(string[] args) 
    { 
     int[][,] newArray = new int[2][,]; 

     int[][,] waypoints = new int[4][,] 
     { 
      new int[,] {{6,3,4,5,6}}, 
      new int[,] {{1,3,4,5,6}}, 
      new int[,] {{1,4,3,2,1}}, 
      new int[,] {{6,3,4,5,6}} 
     }; 

     int l = 0; 
     int m = 0; 

     for (int i = 0; i < waypoints.Length; i++) 
     { 
      for (int j = 0; j < waypoints[i].GetLength(0); j++) 
      { 
       for (int k = 0; k < waypoints[i].GetLength(1); k++) 
       { 
        if (k == 1 || k == 3) 
        { 
         // waypoints[i][j,k].CopyTo(newArray[i][j,k]); 
        } 
        l++; 
        m++; 
       } 
      } 
     } 
     Console.ReadKey(); 
    } 

,我需要從每個交錯數組只[0,1]和[0提取物, 3] 2D數組並將其存儲在新的鋸齒陣列 - newArray中。請,你能幫助我,怎麼做。提前謝謝了。

所需的輸出應該是這樣的:

int[][,] newArray = new int[2][,]; 
{ 
    new int[,] {{3,5}}, 
    new int[,] {{3,5}}, 
    new int[,] {{4,2}}, 
    new int[,] {{3,5}} 
}; 
+0

嘗試這樣的事情'的foreach(INT我在newArray [0,1] {//做的東西}' –

+0

目前還不清楚要如何將數據存儲在'newArray',你能描述一下生成的數組應該是? –

+0

我已經在我的任務中添加了所需的輸出 – SmithiM

回答

1

試試這個。請注意,我擴大了newArray的大小以容納4個2D數組。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication26 
{ 
    class Program 
    { 
     static void Print3DArr(int[][,] arr) 
     { 
      foreach(var TwoDArr in arr) 
      { 
       for (int lineInd = 0; lineInd < TwoDArr.GetLength(0); lineInd++) 
       { 
        for (int elemInd = 0; elemInd < TwoDArr.GetLength(1); elemInd++) 
        { 
         Console.Write(TwoDArr[lineInd, elemInd] + " "); 
        } 
        Console.WriteLine(); 
       } 
       Console.WriteLine(); 
       Console.WriteLine(); 
      } 
     } 

     static void Main(string[] args) 
     { 
      int[][,] newArray = new int[4][,]; 

      int[][,] waypoints = new int[4][,] 
      { 
       new int[,] {{6,3,4,5,6}}, 
       new int[,] {{1,3,4,5,6}}, 
       new int[,] {{1,4,3,2,1}}, 
       new int[,] {{6,3,4,5,6}} 
      }; 

      Print3DArr(waypoints); 

      for (int TwoDArrIndex = 0; TwoDArrIndex < waypoints.Length; TwoDArrIndex++) 
      { 
       newArray[TwoDArrIndex] = new int[waypoints[TwoDArrIndex].GetLength(0), 2]; 

       for (int LineIn2DArr = 0; LineIn2DArr < waypoints[TwoDArrIndex].GetLength(0); LineIn2DArr++) 
       { 
        newArray[TwoDArrIndex][LineIn2DArr, 0] = waypoints[TwoDArrIndex][LineIn2DArr, 1]; 
        newArray[TwoDArrIndex][LineIn2DArr, 1] = waypoints[TwoDArrIndex][LineIn2DArr, 3]; 
       } 
      } 

      Print3DArr(newArray); 
      Console.ReadKey(); 
     } 
    } 
} 
+0

這正是我所期待的。非常感謝您的時間彼得! – SmithiM

+0

不客氣:-)。 –