【问题标题】:C# Passing and returning a multidimensional arrayC# 传递和返回多维数组
【发布时间】:2013-07-27 10:23:32
【问题描述】:

我有一个二维数组,我用数字随机填充。我为此编写的代码可以正常工作,但是,为了更好地组织我的代码,我想将“用数字随机填充”部分放入一个方法中。

数组是从 Main() 方法创建的,因为我计划将数组传递给/从其他操作它的方法返回。然后我尝试编写填充数组的方法,但我不确定如何传递多维数组或返回一个。根据 MSDN,我需要使用“out”而不是 return。

这是我迄今为止尝试过的:

    static void Main(string[] args)
    {
            int rows = 30;
            int columns = 80;



            int[,] ProcArea = new int[rows, columns];

            RandomFill(ProcArea[], rows, columns);

    }

    public static void RandomFill(out int[,] array, int rows, int columns)
    {

        array = new int[rows, columns];


        Random rand = new Random();
        //Fill randomly
        for (int r = 0; r < rows; r++)
        {
            for (int c = 0; c < columns; c++)
            {
                if (rand.NextDouble() < 0.55)
                {
                array[r, c] = 1;
            }
            else
            {
                array[r, c] = 0;
            }
        }
    }

这些是我遇到的错误:

"The best overloaded method match for 'ProcGen.Program.RandomFill(out int[*,*], int, int)' has some invalid arguments"
"Argument 1: cannot convert from 'int' to 'out int[*,*]'"

我做错了什么,我可以做些什么来修复这些错误?另外,我的想法是否正确,因为我正在使用“out”,我需要做的就是:

RandomFill(ProcArea[], rows, columns);

而不是?:

ProcArea = RandomFill(ProcArea[], rows, columns);

有没有调用方法的正确方法?

【问题讨论】:

    标签: c# multidimensional-array parameter-passing return-value


    【解决方案1】:

    您的代码中不需要 out 参数。

    数组是passed by reference,直到在方法中使用新引用对其进行初始化。

    因此,在您的方法中,如果您不使用新引用对其进行初始化,那么您可以不使用 out 参数,并且值将反映在原始数组中 -

    public static void RandomFill(int[,] array, int rows, int columns)
    {
    
        array = new int[rows, columns]; // <--- Remove this line since this array
                                        // is already initialised prior of calling
                                        // this method.
        .........
    }
    

    【讨论】:

      【解决方案2】:

      试试:

      RandomFill(out ProcArea, rows, columns);
      

      【讨论】:

        【解决方案3】:

        Out 参数也需要在调用方显式指定为out

        RandomFill(out ProcArea[], rows, columns);
        

        【讨论】:

          【解决方案4】:

          试试吧...它有效:)

          using System;
          class system
          {
              static void Main(string[] args)
              {
                      int rows = 5;
                      int columns = 5;
          
          
                      int[,] ProcArea = new int[rows, columns];
          
                      RandomFill(out ProcArea, rows, columns);
          
                  // display new matrix in 5x5 form
                      int i, j;
                      for (i = 0; i < rows; i++)
                      {
                          for (j = 0; j < columns; j++)
                              Console.Write("{0}\t", ProcArea[i, j]);
                          Console.WriteLine();
                      }
                      Console.ReadKey();
          
              }
          
              public static void RandomFill(out int[,] array, int rows, int columns)
              {
          
                  array = new int[rows, columns];
          
          
                  Random rand = new Random();
                  //Fill randomly
                  for (int r = 0; r < rows; r++)
                  {
                      for (int c = 0; c < columns; c++)
                      {
                          if (rand.NextDouble() < 0.55)
                          {
                              array[r, c] = 1;
                          }
                          else
                          {
                              array[r, c] = 0;
                          }
                      }
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2011-07-20
            • 2017-03-27
            • 1970-01-01
            • 1970-01-01
            • 2012-04-25
            • 1970-01-01
            • 2010-09-30
            • 1970-01-01
            • 2015-05-30
            相关资源
            最近更新 更多