如何比较两个数组列表? [英] How to compare two array lists ?

查看:91
本文介绍了如何比较两个数组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个不同大小的数组列表。假设list1有4个值,list 2有两个值。我的问题是如何检查list1中是否存在列表2值?

Hi I have two array lists of different sizes. suppose list1 have 4 values and list 2 have two values. My question is "how to check whether list 2 values are present in list1 or not ?

推荐答案

检查一下。我想你会得到你的解决方案。



代码:



Check this. I think you will get your solution.

code:

ArrayList numList1 = new ArrayList(new int[] { 1, 2, 3, 4});
            ArrayList numList2 = new ArrayList(new int[] { 1, 5, 3});


            ArrayList numListDiff3 = new ArrayList();
            foreach (int listElement in numList1)
            {
                if (!numList2.Contains(listElement))
                {
                    numListDiff3.Add(listElement);
                }
            }


            foreach (int listElement in numList2)
            {
                if (!numList1.Contains(listElement))
                {
                    numListDiff3.Add(listElement);
                }
            }



来源: http://forums.asp.net/post/3573258.aspx [ ^ ]



参考原始答案添加[/ Edit]


Source: http://forums.asp.net/post/3573258.aspx[^]

Reference to original answer added[/Edit]


这样的东西:

Something like this:
ArrayList list1 = new ArrayList(new int[] {1,2,3,4,5});
ArrayList list2 = new ArrayList(new int[] {1,2,8});
for (int i=0; i<list1.count && i<list2.Count; i++) {
    if (!Object.Equals(list1[i], list2[i]))
        Console.WriteLine("Different value at index {0}.", i);
}


请参阅以下链接,这可能对您有所帮助



http://stackoverflow.com/questions/4153120/whats-最快的方式 - 比较 - 两个阵列 - 平等 [ ^ ]

http://stackoverflow.com/questions/683310/getting-the-diff-between-two-arrays-in-c [ ^ ]

谢谢

-RA
See the below links, this may helps you

http://stackoverflow.com/questions/4153120/whats-the-fastest-way-to-compare-two-arrays-for-equality[^]
http://stackoverflow.com/questions/683310/getting-the-diff-between-two-arrays-in-c[^]
Thanks
-RA


这篇关于如何比较两个数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆