填充数组的正确方法是什么 - 只有第一行和第一列? 填充Edge数组(这是一维的)工作正常,我尝试以相同的方式填充Matrix数组,但这不起作用。
static void Main(string[] args)
{
Random rn = new Random();
Graph gr = new Graph(rn.Next(1, 30), rn.Next(1, 30));
Console.ReadKey();
}
public class Graph
{
private int[] Edge;
private int n;
private int m;
private int x;
private int[][] Matrix;
public Graph(int _edge, int _matrix)
{
Random rn = new Random();
x = rn.Next(0,30);
Edge=new int[_edge];
Matrix = new int[_matrix][];
for (int i = 1; i < x; i++)
{
Edge[i] = i;
Console.WriteLine(Edge[i]);
}
Console.WriteLine("Number of nodes {0}", x);
for (int i = 0; i < x; i++)
{
for (int j = 0; j < x; j++)
{
Matrix[0][j] = j;
Matrix[i][0] = i;
Console.WriteLine(Matrix[i][j]);
}
}
}
}
答案 0 :(得分:1)
&#34;这不起作用&#34;可能意味着,你得到一个NullReferenceException。 之后
Matrix = new int[_matrix][];
插入
for (int i = 0; i < Matrix.Length; ++i)
Matrix[i] = new int[_matrix];