英文:
Convert string array to 2d Array
问题
以下是您要翻译的代码部分:
I have the following string array comprising of geo-coordinates
32.28057860000054 -174.8307036999996,
33.03439570061262 -174.82691533618038,
33.15555942151208 -174.8255452254468,
33.161189722126196 -174.825486109871,
33.16405200490681 -174.82544919191812,
33.22284105148913 -174.824784409563,
33.32412188155387 -174.82338459780988,
33.341040408962115 -174.8231663812863
...............
I want to convert the above to a 2d-array and also swap the values
e.g.
{
{ -174.8307036999996, 32.28057860000054 },
{ -174.82691533618038, 33.03439570061262 }
}
I know how to create a 2d-array from scratch
string[,] name = new string[,]
{
{ "damien", "kettle" },
{ "joe", "bloggs" },
{ "pat", "murphy" },
{ "tom", "jones" }
};
but not able to figure out how I can convert the original list of coordinates?
Suggestions please?
英文:
I have the following string array comprising of geo-coordinates
32.28057860000054 -174.8307036999996,
33.03439570061262 -174.82691533618038,
33.15555942151208 -174.8255452254468,
33.161189722126196 -174.825486109871,
33.16405200490681 -174.82544919191812,
33.22284105148913 -174.824784409563,
33.32412188155387 -174.82338459780988,
33.341040408962115 -174.8231663812863
...............
I want to convert the above to a 2d-array and also swap the values
e.g.
{
{ -174.8307036999996, 32.28057860000054 },
{ -174.82691533618038, 33.03439570061262 }
}
I know how to create a 2d-array from scratch
string[,] name = new string[,]
{
{ "damien", "kettle" },
{ "joe", "bloggs" },
{ "pat", "murphy" },
{ "tom", "jones" }
};
but not able to figure out how I can convert the original list of coordinates?
Suggestions please?
答案1
得分: 1
double[,] coordinates2D = new double[coordinates.Length, 2];
for (int i = 0; i < coordinates.Length; i++)
{
string[] parts = coordinates[i].Split(' ');
double lat = double.Parse(parts[0]);
double lon = double.Parse(parts[1]);
coordinates2D[i, 0] = lon;
coordinates2D[i, 1] = lat;
}
Or
// 将坐标拆分成字符串的交错数组
string[][] splitCoordinates = coordinates.Select(c => c.Split(' ')).ToArray();
// 创建一个包含双精度值的2D数组,并交换数值
double[,] result = new double[splitCoordinates.Length, 2];
for (int i = 0; i < splitCoordinates.Length; i++)
{
result[i, 0] = double.Parse(splitCoordinates[i][1]);
result[i, 1] = double.Parse(splitCoordinates[i][0]);
}
英文:
double[,] coordinates2D = new double[coordinates.Length, 2];
for (int i = 0; i < coordinates.Length; i++)
{
string[] parts = coordinates[i].Split(' ');
double lat = double.Parse(parts[0]);
double lon = double.Parse(parts[1]);
coordinates2D[i, 0] = lon;
coordinates2D[i, 1] = lat;
}
Or
// Split the coordinates into a jagged array of strings
string[][] splitCoordinates = coordinates.Select(c => c.Split(' ')).ToArray();
// Create a 2D array of doubles and swap the values
double[,] result = new double[splitCoordinates.Length, 2];
for (int i = 0; i < splitCoordinates.Length; i++)
{
result[i, 0] = double.Parse(splitCoordinates[i][1]);
result[i, 1] = double.Parse(splitCoordinates[i][0]);
}
答案2
得分: 0
这部分代码的翻译如下:
也许这会起作用
var text = """
32.28057860000054 -174.8307036999996,
33.03439570061262 -174.82691533618038,
33.15555942151208 -174.8255452254468,
33.161189722126196 -174.825486109871,
33.16405200490681 -174.82544919191812,
33.22284105148913 -174.824784409563,
33.32412188155387 -174.82338459780988,
33.341040408962115 -174.8231663812863
""";
var values = text.Split(",", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(l => l.Split(" "))
.Select(a => new double[] { double.Parse(a.Last()), double.Parse(a.First())});
英文:
Maybe this would work
var text = """
32.28057860000054 -174.8307036999996,
33.03439570061262 -174.82691533618038,
33.15555942151208 -174.8255452254468,
33.161189722126196 -174.825486109871,
33.16405200490681 -174.82544919191812,
33.22284105148913 -174.824784409563,
33.32412188155387 -174.82338459780988,
33.341040408962115 -174.8231663812863
""";
var values = text.Split(",", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(l => l.Split(" "))
.Select(a => new double[] { double.Parse(a.Last()), double.Parse(a.First())});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论