0

我正在开发一个 C# 程序,该程序必须将随机 10*12 二维数组的元素复制到一维数组上。一切似乎都运行良好。但是,2D 数组的某些元素(最后 18 个)不会复制到 1D 数组。

这是代码:

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

namespace James_Archbold_A1
{
    class Program
    {

        static void Main(string[] args)
        {
            int row = 10;
            int column = 12;

            int[,] twoDArray = new int[row, column];  
            int[] oneDArray = new int[row*column];

            FillTwoDimArray(twoDArray);
            DisplayTwoDimArray(twoDArray);
            StoreValues(twoDArray, oneDArray);
            DisplayOneDimArray(oneDArray);
            Console.ReadLine();

        }

        static void FillTwoDimArray(int[,] table)
        {
            int min = 0;
            int max = 100;
            int rndNumber;
            Random rnd = new Random();
            for (int row = 0; row < table.GetLength(0); row++) //use GetLength(0) to get the size of the row
            {
                for (int col = 0; col < table.GetLength(1); col++)  //use GetLength(1) to get the size of the column
                {
                    rndNumber = rnd.Next(min,max);
                    table[row, col] = rndNumber;
                }
            }
        }

        static void DisplayTwoDimArray(int[,] table)
        {
            for (int row = 0; row < table.GetLength(0); row++)
            {
                for (int col = 0; col < table.GetLength(1); col++)
                {
                    Console.Write("{0}\t", table[row, col]);
                }
            }
            Console.WriteLine();
        }

        static void StoreValues(int[,] twoDArray, int[] oneDArray)
        {
            int rowSize = twoDArray.GetLength(0);
            int colSize = twoDArray.GetLength(1);
            for (int row = 0; row < rowSize; row++)
            {
                for (int col = 0; col < colSize; col++)
                {
                    int element;
                    element = twoDArray[row, col];
                    oneDArray[row * rowSize + col] = element;
                }
            }

        }

        static void DisplayOneDimArray(int[] oneDArray)
        {
            for (int i = 0; i < oneDArray.GetLength(0); i++ )
            {
                Console.Write("{0}\t", oneDArray[i] );
            }
            Console.WriteLine();

        }




    }
}
4

2 回答 2

4

如果您有一个 2x5 数组,则您的rowSize为 2,您的colSize为 5。然后您的循环将在数组中设置一个值[row * rowSize + col]。前几个值将是:

0*2+0 = 0
0*2+1 = 1
0*2+2 = 2
0*2+3 = 3
0*2+4 = 4
1*2+0 = 2
1*2+1 = 3
1*2+2 = 4
1*2+3 = 5

因此,您正在循环相同的值,多​​次设置它们,也没有设置数组中的最后一个值。如果您的行数多于列数,我想您会遇到越界异常吗?

如果您更改row * rowSize + col为正确的映射row * colSize + col,它应该可以工作。

于 2013-06-17T02:09:43.247 回答
0

尝试以下代码复制到一维:

void StoreValues(int[,] twoDArray, int[] oneDArray)
{
  int rowSize = twoDArray.GetLength(0);
  int colSize = twoDArray.GetLength(1);
  var total=rowSize*colSize;//TOTAL ITEMS IN 1D ARRAY
  for (int row = 0, d=0; row < rowSize; row++)
  {
      for (int col = 0; col < colSize; col++)
      {
          //STORE AT POSITION d
          oneDArray[d++] = twoDArray[row, col];
      }
  }
}

使用row * rowSize + col将导致此答案中解释的问题。

于 2013-06-17T02:16:37.127 回答