【问题标题】:C# Passing double array to constructor overload methodC#将双数组传递给构造函数重载方法
【发布时间】:2013-03-05 07:09:49
【问题描述】:

我正在从一本书中学习 c#,作为练习的一部分,我必须自己编写代码。要做的一件事是将双精度数组传递给将进一步处理它的构造函数重载方法之一。问题是我不知道该怎么做。

这里是完整的代码(到现在):

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

namespace assignment01v01
{

    public class Matrix
    {
        int row_matrix; //number of rows for matrix
        int column_matrix; //number of colums for matrix
        int[,] matrix;

        public Matrix() //set matrix size to 0*0
        {
            matrix = new int[0, 0];
            Console.WriteLine("Contructor which sets matrix size to 0*0 executed.\n");
        }

        public Matrix(int quadratic_size) //create quadratic matrix according to parameters passed to this constructor
        {
            row_matrix = column_matrix = quadratic_size;
            matrix = new int[row_matrix, column_matrix];
            Console.WriteLine("Contructor which sets matrix size to quadratic size {0}*{1} executed.\n", row_matrix, column_matrix); 
        }

        public Matrix(int row, int column) //create n*m matrix according to parameters passed to this constructor
        {
            row_matrix = row;
            column_matrix = column;
            matrix = new int[row_matrix, column_matrix];
            Console.WriteLine("Contructor which sets matrix size {0}*{1} executed.\n", row_matrix, column_matrix);
        }

        public Matrix(int [,] double_array) //create n*m matrix and fill it with data passed to this constructor
        {
            matrix = double_array;
            row_matrix = matrix.GetLength(0);
            column_matrix = matrix.GetLength(1);
        }

        public int countRows()
        {
            return row_matrix;
        }

        public int countColumns()
        {
            return column_matrix;
        }

        public float readElement(int row, int colummn)
        {
            return matrix[row, colummn];
        }
    }
 

    class Program
    {
        static void Main(string[] args)
        {
            Matrix mat01 = new Matrix();

            Matrix mat02 = new Matrix(3);

            Matrix mat03 = new Matrix(2,3);

            //Here comes the problem, how should I do this?
            Matrix mat04 = new Matrix ( [2,3] {{ 1, 2 }, { 3, 4 }, { 5, 6 }});           

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

        }
    }
}

困扰我的部分代码标有“//问题来了,我该怎么做?”。

欢迎提出任何建议。

【问题讨论】:

    标签: c# arrays multidimensional-array constructor overloading


    【解决方案1】:

    您似乎正在为如何创建具有一组初始值的多维数组而苦苦挣扎。其语法如下

    new [,] {{ 1, 2 }, { 3, 4 }, { 5, 6 }} 
    

    因为在这种情况下您正在初始化数组,所以您不需要指定大小或类型。编译器会从提供的元素中推断出来

    【讨论】:

      【解决方案2】:

      可以如下创建一个多维数组。

       new Matrix(new int[,] {{1, 2, 3,}, {1, 2, 3}});
      

      int 甚至是多余的,所以你可以让它更容易(或者,至少,它应该更容易阅读:))

       new Matrix(new [,] {{1, 2, 3,}, {1, 2, 3}});
      

      【讨论】:

      • 该死的“你是人类吗”对话框让我很忙.... Jared 所说的... :) PS:是的!我是人!
      【解决方案3】:

      您只是切换了索引,并且缺少 new 关键字。这应该有效:

      Matrix mat04 = new Matrix ( new [3,2] {{ 1, 2 }, { 3, 4 }, { 5, 6 }});
      

      或者,正如@JaredPar 所说,您可以完全省略数组大小并让编译器为您推断:

      Matrix mat04 = new Matrix ( new [,] {{ 1, 2 }, { 3, 4 }, { 5, 6 }});
      

      【讨论】:

      • 是的,我选择了解决方案:Matrix mat04 = new Matrix (new [,] {{ 1, 2 }, { 3, 4 }, { 5, 6 }});
      猜你喜欢
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2011-08-31
      • 1970-01-01
      • 2020-07-27
      • 2015-10-11
      • 2011-11-27
      相关资源
      最近更新 更多