两个ArrayList的操作 [英] Two ArrayList manipulation
问题描述
我在会话的一个ArrayList中,可以说,例如 [305306380]
。
I have one ArrayList in Session, lets say for example [305,306,380]
.
在提交,我将它们保存在第2个数组用户选择其他产品,例如 [390305480380]
On submit, user choice other products which i save them in 2nd array, for example [390,305,480,380]
我怎样才能让另外三个阵列,其中
How can i make another three arrays where
-
所有新值
[390480]
所有的值这是在两个列表 [305380]
All values which are in both lists [305,380]
从list1的,不能在列表2中的所有值 [306]
All values from list1 which are not in list2 [306]
我需要这在ASP.NET 4.0 C#
I need this in ASP.NET 4.0 C#
推荐答案
您可以使用的 ArrayList.ToArray()
得到数组对你的ArrayList。使用LINQ然后你就可以轻松地得到你想要与 <$什么C $ C>除了 的 相交
方法,例如:
You can use ArrayList.ToArray()
to get arrays against your arraylists. Then using LINQ you can easily get what you want withExcept
an Intersect
methods, for example
array2.Except(array1)
array1.Except(array2)
array1.Intersect(array2)
编辑:完整code
根据您的要求,您的code可查找这样的;
According to your requirement, your code may look-like this;
ArrayList arrayList1 = new ArrayList(new int[] { 305, 306, 380 });
ArrayList arrayList2 = new ArrayList(new int[] { 390, 305, 480, 380 });
int[] array1 = (int[])arrayList1.ToArray(typeof(int));
int[] array2 = (int[])arrayList2.ToArray(typeof(int));
//1. All New values
int[] uniqueInArray2 = array2.Except(array1).ToArray();
//2. Common values
int[] commonValues = array1.Intersect(array2).ToArray();
//3. Values of arrayList1 which are not in arrayList2
int[] uniqueInArray1 = array1.Except(array2).ToArray();
这篇关于两个ArrayList的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!