两个var数组列表之间的一种联合 [英] A kind of union between two var array lists

查看:63
本文介绍了两个var数组列表之间的一种联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy,我们可以说var(实际上还有更多)这个:

var goog = new ArrayList(){goog_trades [i],trading_dates_goog [i],buysell_goog [i ]};

var aapl = new ArrayList(){appl_trades [i],trading_dates_aapl [i],buysell_aapl [i]};

此变量的数据类似于这个:

goog = [(100,101,102 ...))(date1,date2 ....),(+ 1,-1 ....)

Appl相同的

我想要的结构不是这样的:

goog [0] goog [1] ....

Appl [] Appl [1] .....

我通过垂直方式对数据进行重新整理:

goog 100 date1 +1

101 date2 -1

102 date3 +1

Appl ...相同...







任何想法?

我忘了说for循环不是一个好的想法:D

Tnks a很多:P

解决方案

之后对你的代码感兴趣,我的直觉表明你想要的是每个股票都是交易的集合,每个交易都有识别信息和其他数据,我认为你的问题特别询问在某些方面,如何以维持股票独立性的方式合并这些集合。



我首先创建一个模拟交易的类,而不是使用较旧的'ArrayList数据结构,它只是一个桶,您可以将任何类型的对象放入其中:

  public   class 交易
{
public string 名称;
public int id;
public DateTime dt;
public int i1;
public int i2;

public Trade()
{
// 供将来使用
}
}

然后我会对每个股票将拥有的交易集合进行原型设计:

列表< Trade> Goog =  new  List< Trade>(); 
列表< Trade> Aapl = new 列表< Trade>();

让我们创建一些'Trade:

 Goog的测试实例.Add( new 交易{name =   Google,id =  100 ,dt = DateTime.Now,i1 =  1 ,i2 =  2 }); 
Goog.Add( new 交易{name = Google,id = 101 ,dt = DateTime.Now,i1 = 99 ,i2 = 100 });
Aapl.Add( new 交易{name = Apple,id = 102 ,dt = DateTime.Now,i1 = 3 ,i2 = 4 });

现在让我们考虑在某种程度上需要合并不同的股票。由于原子元素是交易类型的列表,因此这看起来很自然:

列表< List< Trade>>我们希望合并,我们可以做类似的事情:

 Timesdes = new List< List< Trade>> {Goog,Aapl}; 

或者,当然,您可以预先初始化',然后根据需要使用'添加'和'删除'。


LINQ提供了一个 UNION 命令,它允许你组合两个数组。


Hy , lets say we have to var like(in reality there are more) this :
var goog = new ArrayList() { goog_trades[i], trading_dates_goog[i], buysell_goog[i] };
var aapl = new ArrayList() { appl_trades[i], trading_dates_aapl[i],buysell_aapl[i] };
The data this vars is something like this :
goog=[(100,101,102...))(date1, date2....) ,(+1,-1....)
Appl the same
Whell i wan't a structure something like this :
goog[0] goog[1]....
Appl[] Appl[1].....
I meand agregated data by vertical for exaple:
goog 100 date1 +1
101 date2 -1
102 date3 +1
Appl ...the same ...



Any ideea?
I forgot to say that for loops aren't a good ideea :D
Tnks a lot :P

解决方案

After looking at your code, my intuition suggests that what you want is for each stock to be a collection of "trades," where each trade has identifying information, and other data, and I think your question specifically asks how, at some point, to merge those collections in a way that maintains the "independence" of the stocks.

I'd start off by creating a Class that models a trade, rather than using the older 'ArrayList data structure which is just a "bucket" into which you can throw objects of any type:

public class Trade
{
    public string name;
    public int id;
    public DateTime dt;
    public int i1;
    public int i2;

    public Trade()
    {
        // for future use
    }
}

I'd then prototype the collections of trades that each stock will have:

List<Trade> Goog = new List<Trade>();
List<Trade> Aapl = new List<Trade>();

Let's create a few test instances of 'Trade:

Goog.Add (new Trade { name = "Google", id = 100, dt =DateTime.Now, i1 = 1, i2 = 2 });
Goog.Add (new Trade { name = "Google", id = 101, dt = DateTime.Now, i1 = 99, i2 = 100 });
Aapl.Add (new Trade { name = "Apple", id = 102, dt = DateTime.Now, i1 = 3, i2 = 4 });

Now let's consider the need to, at some point, merge different stocks. Since the "atomic" elements are Lists of type 'Trade, then this seems natural:

List<List<Trade>> AllTrades;

At the moment we wish to merge we could do something like:

AllTrades = new List<List<Trade>> { Goog, Aapl };

Or, of course, you could pre-initialize 'AllTrades, and just use 'Add, and 'Remove, as necessary.


LINQ provides a UNION command that will allow you to combine two arrays.


这篇关于两个var数组列表之间的一种联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆