如何在调用的 class 中正确初始化二维数组
How to correctly initialize a 2D array in a called class
这是抛出 EdgeList
未初始化的错误。这是 class(相关部分在底部):
public class TacLineStruct
{
// The number of unit groups in the Army
public int NumGroups
{
get
{
return _NumGroups;
}
set
{
_NumGroups = value;
}
}
private int _NumGroups;
// The number of edges,
public int NumEdges
{
get
{
return _NumEdges;
}
set
{
_NumEdges = value;
}
}
private int _NumEdges;
// The number of units below the threshold
public int NumBelowThreshold
{
get
{
return _NumBelowThreshold;
}
set
{
_NumBelowThreshold = value;
}
}
private int _NumBelowThreshold;
// The specific Group that a unit belongs to
public int[] GroupID
{
get;
set;
}
// The list of all the edges
public int[][] EdgeList
{
get;
set;
}
// The list of all the edge weights
public float[] EdgeWeight
{
get;
set;
}
// The geographical center of each group
public Point[] GroupCenter
{
get;
set;
}
public TacLineStruct(int arrayLength)
{
GroupID = new int[arrayLength];
int[,] EdgeList = new int[(arrayLength * arrayLength),2];
EdgeWeight = new float[arrayLength * arrayLength];
GroupCenter = new Point[arrayLength];
}
}
这就是我调用和初始化它的方式(片段):
TacLineStruct TLS = new TacLineStruct(Army.Count);
for (int i = 0; i <= Army.Count; i++)
{
for (int j = i + 1; j <= Army.Count; j++)
{
TLS.EdgeList[NumEdges][0] = i; /* first vertex of edge */
TLS.EdgeList[NumEdges][1] = j; /* second vertex of edge */
// ...
}
}
我收到 EdgeList
未初始化的运行时错误。我最好的猜测是我没有正确使用运行时设置长度的二维数组。
在你的构造函数中,你正在做:
int[,] EdgeList = new int[(arrayLength * arrayLength), 2];
创建一个与字段同名的新(本地)变量。相反,你应该这样做:
this.EdgeList = new int[(arrayLength * arrayLength), 2];
你可以省略this
,但它可以防止你再次犯同样的错误。
此外,您应该将字段声明更改为
public int[,] EdgeList
然后您可以通过以下方式设置数组中的各个字段:
EdgeList[i,j] = value;
这是抛出 EdgeList
未初始化的错误。这是 class(相关部分在底部):
public class TacLineStruct
{
// The number of unit groups in the Army
public int NumGroups
{
get
{
return _NumGroups;
}
set
{
_NumGroups = value;
}
}
private int _NumGroups;
// The number of edges,
public int NumEdges
{
get
{
return _NumEdges;
}
set
{
_NumEdges = value;
}
}
private int _NumEdges;
// The number of units below the threshold
public int NumBelowThreshold
{
get
{
return _NumBelowThreshold;
}
set
{
_NumBelowThreshold = value;
}
}
private int _NumBelowThreshold;
// The specific Group that a unit belongs to
public int[] GroupID
{
get;
set;
}
// The list of all the edges
public int[][] EdgeList
{
get;
set;
}
// The list of all the edge weights
public float[] EdgeWeight
{
get;
set;
}
// The geographical center of each group
public Point[] GroupCenter
{
get;
set;
}
public TacLineStruct(int arrayLength)
{
GroupID = new int[arrayLength];
int[,] EdgeList = new int[(arrayLength * arrayLength),2];
EdgeWeight = new float[arrayLength * arrayLength];
GroupCenter = new Point[arrayLength];
}
}
这就是我调用和初始化它的方式(片段):
TacLineStruct TLS = new TacLineStruct(Army.Count);
for (int i = 0; i <= Army.Count; i++)
{
for (int j = i + 1; j <= Army.Count; j++)
{
TLS.EdgeList[NumEdges][0] = i; /* first vertex of edge */
TLS.EdgeList[NumEdges][1] = j; /* second vertex of edge */
// ...
}
}
我收到 EdgeList
未初始化的运行时错误。我最好的猜测是我没有正确使用运行时设置长度的二维数组。
在你的构造函数中,你正在做:
int[,] EdgeList = new int[(arrayLength * arrayLength), 2];
创建一个与字段同名的新(本地)变量。相反,你应该这样做:
this.EdgeList = new int[(arrayLength * arrayLength), 2];
你可以省略this
,但它可以防止你再次犯同样的错误。
此外,您应该将字段声明更改为
public int[,] EdgeList
然后您可以通过以下方式设置数组中的各个字段:
EdgeList[i,j] = value;