c#有两个数组,想把这个两组中相同的元素放在另一个数组中
例如:str1={"1","2","4","9","6","23","12","28","10","5","15",30","19","22"};str2={"1","4...
例如:
str1={"1","2","4","9","6","23","12","28","10","5","15",30","19","22"};
str2={"1","4","2","6","30","26","33","19","27","42","44"};
想把这两个数组中相同元素,比如 "1","2","4","6","19","30" 这几个元素放到一个新的数组,比如strSame中,str1中的剩下那些和str2不同的也放到一个新的数组中,比如
str1P={"9","23","12","28","10","5","22"};,同样的str2中的那些和str1不同的也放到一个新的数组中,比如str2P={"26","33","27","42","44"};
麻烦各位大侠帮帮忙,我刚学C#,水平有点菜,感激不尽啊 展开
str1={"1","2","4","9","6","23","12","28","10","5","15",30","19","22"};
str2={"1","4","2","6","30","26","33","19","27","42","44"};
想把这两个数组中相同元素,比如 "1","2","4","6","19","30" 这几个元素放到一个新的数组,比如strSame中,str1中的剩下那些和str2不同的也放到一个新的数组中,比如
str1P={"9","23","12","28","10","5","22"};,同样的str2中的那些和str1不同的也放到一个新的数组中,比如str2P={"26","33","27","42","44"};
麻烦各位大侠帮帮忙,我刚学C#,水平有点菜,感激不尽啊 展开
展开全部
使用linq,可以一步到位
string[] str1={"1","2","4","9","6","23","12","28","10","5","15","30","19","22"};
string[] str2={"1","4","2","6","30","26","33","19","27","42","44"};
//这是交集
var res = str1.Intersect(str2);
//这是差集
var res1 = str1.Except(res);
var res2 = str2.Except(res);
如果是自己的方法,这使用 Dictionary<string,int>暂存一下,下扫描str1,把所有值放入Dictionary(value设置为1)
然后扫描str2,如果值存在则把value设置为0。如果值不存在则设置为-1
这样二次扫描完成,则所有value为1的就是你的str1p,value为-1的就是你的str2p,而value为0的就是你要求的差值
string[] str1={"1","2","4","9","6","23","12","28","10","5","15","30","19","22"};
string[] str2={"1","4","2","6","30","26","33","19","27","42","44"};
//这是交集
var res = str1.Intersect(str2);
//这是差集
var res1 = str1.Except(res);
var res2 = str2.Except(res);
如果是自己的方法,这使用 Dictionary<string,int>暂存一下,下扫描str1,把所有值放入Dictionary(value设置为1)
然后扫描str2,如果值存在则把value设置为0。如果值不存在则设置为-1
这样二次扫描完成,则所有value为1的就是你的str1p,value为-1的就是你的str2p,而value为0的就是你要求的差值
2012-05-28
展开全部
string[] str1 = new string[] { "1", "2", "4", "9", "6", "23", "12", "28", "10", "5", "15", "30", "19", "22" };
string[] str2 = new string[] { "1", "4", "2", "6", "30", "26", "33", "19", "27", "42", "44" };
string[] str3 = str1.Where(t=>str2.Contains(t)).ToArray();
str3就是你想要的答案
string[] str2 = new string[] { "1", "4", "2", "6", "30", "26", "33", "19", "27", "42", "44" };
string[] str3 = str1.Where(t=>str2.Contains(t)).ToArray();
str3就是你想要的答案
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
\\伪代码
str3=new string [str1长度+str2长度];
for(i=0;i<str1长度;i++)
{
for(j=0;j<str2长度;i++)
{
if(str1[i]=str2[j])
{
str3.add(str1[i])
}
}
}
差不多就是这个原理了 剩下的饿自己想
str3=new string [str1长度+str2长度];
for(i=0;i<str1长度;i++)
{
for(j=0;j<str2长度;i++)
{
if(str1[i]=str2[j])
{
str3.add(str1[i])
}
}
}
差不多就是这个原理了 剩下的饿自己想
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有个笨办法..双重循环,把数组1的每个值拿出来,与数组2的每个值相比较,想同的就加进新数组里
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-05-28
展开全部
做一个阵列试试
追问
能具体说一下吗,最好有代码指导一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询