我正在从一本书学习c#,并且必须自行编写代码作为练习的一部分。其中一件事是将double数组传递给构造函数重载方法之一,后者将进一步处理它。问题是我不知道该怎么做。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 } };
}
}
}
部分困扰我的是标有“//这里说到这个问题,我应该怎么办呢?”。
欢迎任何建议。
该死“你是人类”对话让我忙.... Jared说什么...... :) PS:是的!我是人! – bas 2013-03-16 16:22:59
您的解决方案就像一个魅力,谢谢。 – 2013-03-16 16:39:19