【问题标题】:Copy 2D array from jagged 2D array in into another jagged 2D array将二维数组从锯齿状二维数组复制到另一个锯齿状二维数组中
【发布时间】:2016-07-25 09:15:01
【问题描述】:

我有像这样的锯齿状二维数组:

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] 二维数组,并将其存储在新的锯齿状数组 - newArray 中。拜托,你能帮我吗,怎么做。非常感谢。

所需的输出应如下所示:

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

【问题讨论】:

  • 试试这样的Foreach (int i in newArray [0,1]{//do stuff}
  • 不清楚您想如何将数据存储在newArray 中。你能描述一下结果数组应该是什么吗?
  • 我已经在我的任务中添加了所需的输出。
  • 您的数组有些不寻常,但在我看来,您好像在处理 3D 数组(2D 数组的数组),即使您不需要它们,因为您的 2D 数组元素都只有一个元素每个(一行),即它们是逻辑 POV 的一维;如双花括号所示。
  • 我知道彼得你的意思,但我需要保留这个解决方案用于其他目的。

标签: c# arrays jagged-arrays


【解决方案1】:

试试这个。请注意,我扩展了 newArray 的大小以容纳 4 个二维数组。

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

【讨论】:

  • 这正是我想要的。非常感谢您抽出宝贵时间彼得!!
猜你喜欢
  • 2015-10-18
  • 1970-01-01
  • 2014-03-16
  • 2014-04-21
  • 2011-02-04
  • 1970-01-01
  • 2014-12-26
  • 2014-05-16
相关资源
最近更新 更多