数组一:1,2.3,4,5,6,7,8,9
数组二、1,5,8
有什么好办法取出它们不一样的?
就是:2,3,4,6,7,9现在已经晕菜了!
大家帮忙
数组二、1,5,8
有什么好办法取出它们不一样的?
就是:2,3,4,6,7,9现在已经晕菜了!
大家帮忙
解决方案 »
- 请将以下vb代码如何写为C#代码 谢谢
- 如何通过vs2005把Excel导入SQL
- 给大家出道算法题,测测你数学学的如何
- 怎么实现类似google map 的滚动鼠标的放大和缩小的功能,对一张图片的中心为中心开始放大,缩小?
- ArrayList 对多个字段经行排序的问题
- C# 压缩多个文件
- VS.NET 2005 如何添加 Directx 的COM 组件啊
- 在另一台机器调Webservice问题?
- DataRow找到记录与textBox代码绑定的问题!!!(分不够可以多加)
- 我在用多线程作代理服务器,能用但速度很慢,而且还经常报“远程主机强迫关闭了一个连接的错”,请问错还什么地方?
- 请问要把两条相交的线组合成一个物体怎么实现?
- 上午面试.NET去了,不会的题大家研究下
int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] arr2 = { 1, 5, 8 };
List<int> list = new List<int>();
foreach (int i in arr1)
if (Array.IndexOf(arr2, i) < 0)
list.Add(i);
int[] result = list.ToArray();
int[] arr2 = { 1, 5, 8 };
var arr = from i in arr1
where Array.IndexOf(arr2, i) < 0
select i;
int[] result = arr.ToArray();
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//楼主的两个数组
int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] array2 = { 1, 5, 8 }; //遍历array1,取出每一个元素去跟array2比较,若有相同的元素存在则设置exist为真
foreach (int i in array1)
{
bool exist = false;
foreach(int j in array2)
{
if (i == j)
exist = true;
}
//若exist为假,则输出元素
if (!exist)
Console.Write(i+" ");
}
//暂停
Console.ReadKey();
}
}
}//以上代码在Microsoft Visual C# 2008 速成版 SP1 - 简体中文 中通过
int[] arr1 ={ 1,2,3,4,5,6,7,8,9 };
int[] arr2 = { 1, 5, 8 };
var result = arr1.Where(a=>!arr2.Contains(a));
foreach (var n in result)
{
Console.Write(n+" ");
}
{
class Program
{
static void Main(string[] args)
{
//楼主的两个数组
int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] array2 = { 1, 5, 8 }; //遍历array1,用array2.Contains(i)来判断是否包含i
foreach (int i in array1)
{
if (!array2.Contains(i))
Console.Write(i+" ");
}
//暂停
Console.ReadKey();
}
}
}//以上代码在Microsoft Visual C# 2008 速成版 SP1 - 简体中文 中通过更简洁明了,受ojlovecd前辈的启发。PS:请问如何自动高亮代码?
List<int> group1 = new List<int>();
group1.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
List<int> group2 = new List<int>(new int[] { 1,5,8});
List<int> group3 = new List<int>(); // The number that belong to group1 ,but not belong group2
List<int> group4 = new List<int>(); // The number that belong to group2 ,but not belong group1
foreach (int nu1 in group1)
{
if (!group2.Contains(nu1))
{
group3.Add(nu1);
}
}
foreach (int nu2 in group2)
{
if (!group2.Contains(nu2))
{
group4.Add(nu2);
}
} List<int> groupALL = new List<int>();
groupALL.AddRange(group3);
groupALL.AddRange(group4);
MessageBox.Show(group3.Count.ToString());
MessageBox.Show(group4.Count.ToString());
int[] arr2 = { 1, 5, 8 };
List<int> list = new List<int>(arr1);
foreach (int i in arr2)
list.Remove(i);
int[] result = list.ToArray();
int[] arrint1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] arrint2 = { 1,5,8,45};
int[] retint1 = arrint1.Concat(arrint2).Distinct().ToArray();//合并两数组
int[] retint2 = arrint1.Intersect(arrint2).ToArray(); //取得两数组相同的
int[] retint = retint1.Except(retint2).ToArray(); //去除相同的
int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] arr2 = { 1, 5, 8 };
Array.ForEach<int>(arr1, delegate(int a) {
if(!arr2.Contains(a))
{
res+=a.ToString();
}
});还有linq版的,实际大同小异,就不写了
{
List<int> list = new List<int>();
list.AddRange(a1);
for (int i=0;i<a2.Length ;i++)
{
if (list.Contains (a2[i]))
list.Remove (a2[i]);
}
int[] result = list.ToArray();
return result ;
}
public static int[] FindDifferent(int[] a1, int[] a2)
{
List <int> list = new List <int>();
List <int> list1 = new List <int>();
list.AddRange(a1);
for (int i=0;i <a2.Length ;i++)
{
if (list.Contains(a2[i]))
{
list.Remove(a2[i]);
}
else
{
list1.Add(a2[i]);
}
}
list.AddRange(list1);
int[] result = list.ToArray();
return result ;
}