gpt4 book ai didi

c# - List 中的问题

转载 作者:行者123 更新时间:2023-11-30 12:53:10 25 4
gpt4 key购买 nike

这有什么问题(在 C# 3.0 中):

List<double> x = new List<double> { 0.0330, -0.6463, 0.1226, -0.3304, 0.4764, -0.4159, 0.4209, -0.4070, -0.2090, -0.2718, -0.2240, -0.1275, -0.0810, 0.0349, -0.5067, 0.0094, -0.4404, -0.1212 };
List<double> y = new List<double> { 0.4807, -3.7070, -4.5582, -11.2126, -0.7733, 3.7269, 2.7672, 8.3333, 4.7023,0,0,0,0,0,0,0,0,0 };

List<double[,]> z = new List<double[,]>{x,y}; // this line

产生的错误是:

Error: Argument '1': cannot convert from 'System.Collections.Generic.List<double>' to 'double[*,*]' 

需要帮助。

最佳答案

var z = new List<List<double>> { x, y };

但是,如果您想将两个列表存储在二维数组 ([,]) 中 this is your anwser .您必须手动转换它,如下所示:

public static T[,] To2dArray(this List<List<T>> list)
{
if (list.Count == 0 || list[0].Count == 0)
throw new ArgumentException("The list must have non-zero dimensions.");

var result = new T[list.Count, list[0].Count];
for(int i = 0; i < list.Count; i++)
{
for(int j = 0; j < list.Count; j++)
{
if (list[i].Count != list[0].Count)
throw new InvalidOperationException("The list cannot contain elements (lists) of different sizes.");
result[i, j] = list[i][j];
}
}

return result;
}

关于c# - List<double[,]> 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2719794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com