C# 用点数据填充三维阵列(锯齿阵列)
我有一个三维矩阵C# 用点数据填充三维阵列(锯齿阵列),c#,arrays,jagged-arrays,C#,Arrays,Jagged Arrays,我有一个三维矩阵 private int[][][] Matrix 但是我不知道怎么填这个。 第一个维度是我的图片切片 第二个用于一个切片的x值,第三个用于y值 那么有人知道如何用一些数据填充这个数组进行测试吗 谢谢您可以这样做: Matrix = new int[5][][]; // 5 slices Matrix[0] = new int[3][]; // 3 x values for the first slice Matrix[0][0] = new int[2]; // 2 y va
private int[][][] Matrix
但是我不知道怎么填这个。
第一个维度是我的图片切片
第二个用于一个切片的x值,第三个用于y值
那么有人知道如何用一些数据填充这个数组进行测试吗
谢谢您可以这样做:
Matrix = new int[5][][]; // 5 slices
Matrix[0] = new int[3][]; // 3 x values for the first slice
Matrix[0][0] = new int[2]; // 2 y values for the first x value in the first slice
class Slice
{
public IList<XValue> XValues {get; set; }
}
class XValue
{
public IList<YValue> YValues {get; set; }
}
class YValue
{
// ...
}
var slices = new List<Slice>();
但我不认为你应该用这样的东西。它很容易出错
我建议这样做:
Matrix = new int[5][][]; // 5 slices
Matrix[0] = new int[3][]; // 3 x values for the first slice
Matrix[0][0] = new int[2]; // 2 y values for the first x value in the first slice
class Slice
{
public IList<XValue> XValues {get; set; }
}
class XValue
{
public IList<YValue> YValues {get; set; }
}
class YValue
{
// ...
}
var slices = new List<Slice>();
类片
{
公共IList XValues{get;set;}
}
类XValue
{
公共IList YValues{get;set;}
}
类Y值
{
// ...
}
var slices=新列表();
您可以使用数组文字创建数组的数组:
private int[][][] Matrix = {
{
{1,2,3},
{4,5}
},
{
{1,2},
{3},
{4,5,6,7,8},
{9,10}
},
{
{1,2,3}
}
};
请注意,这是一个锯齿状数组,因此不同的子数组可以具有不同的项数。如果需要三维矩阵,可能需要三维数组
int[,]
而不是嵌套的一维数组。为什么需要锯齿状数组?在您的案例中,有一个对象列表会更容易。现在如何添加值?