数组和列表 [英] arrays and lists

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

问题描述

我有两个数组Ref_sequence和Com_sequence,现在我想添加
将两个数组合并为一个数组,有人可以为此建议一个很好的答案吗
在此先感谢:)

I have two arrays namely Ref_sequence and Com_sequence , now i want to add
both arrays into a single array , can anyone suggest me a good answer for this
Thanks in advance :)

推荐答案

CopyTo函数如何?

在此示例中,两者都是int
的数组
How about the CopyTo function

In this example, both are an array of int

int[] combined = new int[Ref_sequence.Length + Com_sequence.Length];
Ref_sequence.CopyTo(combined, 0);
Com_sequence.CopyTo(combined, Ref_sequence.Length);


如果它们是相同类型,则可以:
1)声明一个新的数组,其大小足以容纳它们两个,然后将它们复制过来.
2)声明一个新列表< T>.并为每个数组使用List.AddRange.然后,您可以直接使用列表,或者如果以后需要它们作为数组,则可以使用List.ToArray.
如果它们不是同一类型,则将它们分开-只会使以后变得不整洁.
If they are the same type, then you could:
1) Declare a new array big enough to hold them both, and copy them over.
2) Declare a new list<T> and use List.AddRange for each array. You can then use the list directly or the List.ToArray if you need them as an array later.
If they aren''t the same type, then keep them separate - it will only make things untidy later.


这篇关于数组和列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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