ArrayList Manipulation?

本文关键字:Manipulation ArrayList | 更新日期: 2023-09-27 17:53:42

可以将两个arraylist中的数据存储到<list>中?

这是我的代码与两个数组合并:

ArrayList arrPrices = new ArrayList();
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util(); 
arrPrices = oUtils.GetPrices(SymbolIndex);
ArrayList arrDetails = new ArrayList();
List<StockInfoDetails> lstStockInfoDetails = new List<StockInfoDetails>();
Util oUtils = new Util(); 
arrPrices = oUtils.GetDetails(SymbolIndex);

ArrayList Manipulation?

您可以使用linq简单地做到这一点:

lstStockInfoPrice.AddRange(arr1.Cast<StockInfoPrice>());
lstStockInfoPrice.AddRange(arr2.Cast<StockInfoPrice>());

参见IEnumerable中的 Cast

这是可能的。

如果oUtils.GetPrices(SymbolIndex)返回StockInfoPrice;

lstStockInfoPrice.AddRange(oUtils.GetPrices(SymbolIndex));

如果这个Util类不是你自己的,那么你就只能用马吕斯的答案了。然而,如果你控制了Util类,那么你可以让GetPrices和GetDetails方法分别返回IEnumerable和IEnumerable类型的东西。

然后,您可以使用list . addrange()方法将整个批添加到另一个列表中。

顺便说一句,你在arrPrices声明中的分配是浪费时间——分配的对象永远不会被使用,然后会被垃圾收集。

你的GetPrices()方法返回一个数组列表-即,一个新的数组列表,和

arrPrices = oUtils.GetPrices(SymbolIndex);

只是使arrPrices指向新的列表。然后没有对声明arrPrices时分配的那个的引用,所以它被丢弃了。

这样做:-

ArrayList arrPrices;
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util(); 
arrPrices = oUtils.GetPrices(SymbolIndex);

如果您想将值从arrPrices移动到lstStockInfoPricelstStockInfoDetails,您可以遍历数组列表并将元素放入列表中。像这样:

foreach(var o in arrPrices)
{
  lstStockInfoPrice.Add(o); // or Add((StockInfoPrice)o)
}