简体   繁体   English

将2D阵列从锯齿状2D阵列复制到另一个锯齿状2D阵列中

[英]Copy 2D array from jagged 2D array in into another jagged 2D array

I have jagged 2D array like so: 我有锯齿状的二维数组,像这样:

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();
    }

And I need to extract from each jagged array only [0,1] and [0,3] 2D array and store that in new jagged array - newArray. 而且我需要从每个锯齿状数组中仅提取[0,1]和[0,3] 2D数组,并将其存储在新的锯齿状数组-newArray中。 Please, could you help me, how to do that. 拜托,你能帮助我,怎么做。 Many thanks in advance. 提前谢谢了。

Desired output should look like: 所需的输出应如下所示:

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

Try this. 尝试这个。 Note that I expanded the size of newArray to accomodate 4 2D-arrays. 请注意,我扩大了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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM