C# 数组合并?????

来源:百度知道 编辑:UC知道 时间:2024/06/18 06:07:19
string[] student1 = { "$", "$", "c", "m", "d", "1", "2", "3", "1", "2", "3" };
string[] student2 = { "0", "1", "2", "3", "4", "5", "6", "6", "1", "8", "16","10","45", "37", "82" };
ArrayList student = new ArrayList();
foreach (string s1 in student1)
{
student.Add(s1);
}
foreach (string s2 in student2)
{
student.Add(s2);
}
string[] c = (string[])student.ToArray(typeof(string));

我要把数组student1里面的内容始终加在student2的前面,这样做实现了,但是会出现乱序的情况吗?在这里是这个顺序到别的地方确乱序了,那就不行了,我要保证始终是这样的顺序。

尽量不要使用foreach遍历数组!
string[] student1 = { "$", "$", "c", "m", "d", "1", "2", "3", "1", "2", "3" };
string[] student2 = { "0", "1", "2", "3", "4", "5", "6", "6", "1", "8", "16","10","45", "37", "82" };
string[] student = new string[student1.Length+student2.Length];
for (int i = 0; i < student1.Length; i++)
{
student[i] = student1[i];
}
for (int i = 0; i < student2.Length; i++)
{
student[i+student1.Length] = student2[i];
}

student就是你想要的数组。

用插入操作 没有的话就用哈希表吧
arraylist本来就是数组 你还转化干什么
string[] str=new string[10];
ArrayList list = new ArrayList();
list.Insert(str.Length, " ");

foreach 是 怎么用的?