【发布时间】:2021-10-16 17:45:15
【问题描述】:
我有一个循环遍历数组并将数组转换为浮点列表的forloop。
private List<Single> xfloats;
for (int i = 0; i < DataServerManager.instance.x.Count; i++)
{
xfloats = DataServerManager.instance.x.Select(str => (ok: Single.TryParse(str, out Single f), f)).Where(t => t.ok).Select(t => t.f).ToList();
}
我有另一个名为“tempList”的浮点数列表,我想将“xfloats”的结果添加到 tempList 中。但我收到错误:error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List' to 'float'
List<float> tempList = new List<float>();
tempList.Add(xfloats);
如何将浮点数列表添加到浮点数列表中?
【问题讨论】:
-
也许你正在寻找
List<T>.AddRange。 -
或者可能只是
tempList = new List<float>(xfloats);或tempList = xfloats.ToList();(使用System.Linq)如果它应该只使用给定的数组项初始化列表一次? -
无论如何这应该回答你的问题AddRange to a Collection和Add multiple items to a list
-
谢谢!是的,有效