两个 ArrayList 操作 [英] Two ArrayList manipulation
问题描述
我在 Session 中有一个 ArrayList,例如 [305,306,380]
.
I have one ArrayList in Session, lets say for example [305,306,380]
.
提交时,用户选择我将它们保存在第二个数组中的其他产品,例如 [390,305,480,380]
On submit, user choice other products which i save them in 2nd array, for example [390,305,480,380]
如何制作另外三个数组
所有新值
[390,480]
两个列表中的所有值 [305,380]
list1 中所有不在 list2 中的值 [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()
根据您的数组列表获取数组.然后使用 LINQ,您可以轻松获得您想要的除了
一个相交
方法,例如
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)
完整代码
根据您的要求,您的代码可能看起来像这样;
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屋!