What is wrong with this (in 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
The error produced is:
Error: Argument '1': cannot convert from 'System.Collections.Generic.List<double>' to 'double[*,*]'
开发者_如何学运维
Help needed.
var z = new List<List<double>> { x, y };
However if you want to store your two lists in a twodimensional array ([,]) this is your anwser. You will have to convert it manually as shown there:
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;
}
The collection initializer for List<double[,]>
expects elements of type double[,]
(which is a two-dimensional array, similar to a matrix), but you're passing it x
and y
, which are of type List<double>
, which means it's trying to add two lists of doubles as the elements of the new list.
If you're trying add coordinates to the list, then you need a structure of some sort to contain them. You could write your own or you could use System.Drawing.PointF
.
double[,]
defines a multidimensional array but you are specifying two Lists.
From your Initialization it looks like you are looking for something like
List<PointF> list = new List<PointF> { new PointF (0.0330F, 0.4807F), new PointF (-0.6463F, -3.7070F) };
Shouldn't it be:
List<List<double>,List<double>> z = new List<List<double>, List<double>>{x,y};
But I don't think that is really what you're after is it?
Are you after something like this?
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 = x.Select((x1, index) => new double[2] {x1, y[index]} ).ToList();
EDIT: changed my answer to join the lists on the index correctly instead of looking it up.
精彩评论