如何从 C# 中的 double[] 中提取值?
How can I extract values from double[,] in C#?
我有以下数组:
private static readonly double[,] positionsOfCustomer = new double[,] {
{47.645892, -122.336954}, // Gasworks Park
{47.688741, -122.402965}, // Golden Gardens Park
{47.551093, -122.249266}, // Seward Park
{47.555698, -122.065996}, // Lake Sammamish Park
{47.663747, -122.120879}, // Marymoor Park
{47.857295, -122.316355}, // Meadowdale Beach Park
{47.530250, -122.393055}, // Lincoln Park
{47.503266, -122.200194}, // Gene Coulon Park
{47.591094, -122.226833}, // Luther Bank Park
{47.544120, -122.221673} // Pioneer Park
};
我更喜欢命名值而不是使用注释,所以我尝试使用 Visual Studio 来提取值,例如:
private static readonly object gasworksPark = { 47.645892, -122.336954 };
// etc.
private static readonly double[,] positionsOfCustomer = new double[,] {
gasworksPark,
// etc.
};
但这至少会导致两个错误:
- Can only use array initializer expressions to assign array types. Try a new expression instead.
- A nested array initializer is expected.
如何以编译器能够理解的方式提取这些值?
我不确定如何使用 Multidimensional Array
实现此目的,但可以使用 Jagged Array
:
private static readonly double[] gasworksPark = { 47.645892, -122.336954 };
private static readonly double[] goldenGardensPark = { 47.688741, -122.402965 };
// etc.
private static readonly double[][] positionsOfCustomer = new double [][]{
gasworksPark,
goldenGardensPark
};
你可以试试这个
private static readonly double[,] gasworksPark = {{ 47.645892, -122.336954 }};
private static readonly double[,] goldenGardensPark = {{ 47.688741, -122.402965 }};
//etc...
private static readonly object[] positionsOfCustomer =
{
gasworksPark,
goldenGardensPark,
//etc
};
你也可以使用这个
private Dictionary<Name, Tuple<double, double>> names = new Dictionary<Name, Tuple<double, double>>
{
{ Name.gasworksPark, new Tuple<double, double>( 47.645892, -122.336954 ) },
{ Name.goldenGardensPark, new Tuple<double, double>( 47.688741, -122.402965 ) }
};
private enum Name
{
gasworksPark,
goldenGardensPark
}
我有以下数组:
private static readonly double[,] positionsOfCustomer = new double[,] {
{47.645892, -122.336954}, // Gasworks Park
{47.688741, -122.402965}, // Golden Gardens Park
{47.551093, -122.249266}, // Seward Park
{47.555698, -122.065996}, // Lake Sammamish Park
{47.663747, -122.120879}, // Marymoor Park
{47.857295, -122.316355}, // Meadowdale Beach Park
{47.530250, -122.393055}, // Lincoln Park
{47.503266, -122.200194}, // Gene Coulon Park
{47.591094, -122.226833}, // Luther Bank Park
{47.544120, -122.221673} // Pioneer Park
};
我更喜欢命名值而不是使用注释,所以我尝试使用 Visual Studio 来提取值,例如:
private static readonly object gasworksPark = { 47.645892, -122.336954 };
// etc.
private static readonly double[,] positionsOfCustomer = new double[,] {
gasworksPark,
// etc.
};
但这至少会导致两个错误:
- Can only use array initializer expressions to assign array types. Try a new expression instead.
- A nested array initializer is expected.
如何以编译器能够理解的方式提取这些值?
我不确定如何使用 Multidimensional Array
实现此目的,但可以使用 Jagged Array
:
private static readonly double[] gasworksPark = { 47.645892, -122.336954 };
private static readonly double[] goldenGardensPark = { 47.688741, -122.402965 };
// etc.
private static readonly double[][] positionsOfCustomer = new double [][]{
gasworksPark,
goldenGardensPark
};
你可以试试这个
private static readonly double[,] gasworksPark = {{ 47.645892, -122.336954 }};
private static readonly double[,] goldenGardensPark = {{ 47.688741, -122.402965 }};
//etc...
private static readonly object[] positionsOfCustomer =
{
gasworksPark,
goldenGardensPark,
//etc
};
你也可以使用这个
private Dictionary<Name, Tuple<double, double>> names = new Dictionary<Name, Tuple<double, double>>
{
{ Name.gasworksPark, new Tuple<double, double>( 47.645892, -122.336954 ) },
{ Name.goldenGardensPark, new Tuple<double, double>( 47.688741, -122.402965 ) }
};
private enum Name
{
gasworksPark,
goldenGardensPark
}