开发者

Comparison of items in two ArrayLists and return differences

I'm currently writing a simple app to compare two lists and return differences if any are found.

Note - [int] imaginary index for the purpose of this explanation

//---------------Prep----------------------------
            ArrayList list1 = new ArrayList();
            ArrayList storage = new ArrayList();
            ArrayList list2 = new ArrayList();
            string path = Application.ExecutablePath;
            string root = Path.GetDirectoryName(path);
//---------------END Prep------------------------
//---------------Load content into list1------------------------
StreamReader objReader = new StreamReader(root + "/healthy.txt");
            string sLine = "";

            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                if (sLine != null)
                    list1.Add(sLine);
            }
            objReader.Close();
//---------------END Load content into list1------------------------

//---------------Load content into list2------------------------
 string[] files = Directory.GetFiles(root, "*.txt", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                storage.Add(file.ToString());
            }
            foreach (string sOutput2 in storage)
            {
                list2.Add(GetMD5HashFromFile(sOutput2));
            }

//---------------END Load content into list2------------------------

I tried everything, but it seems that I cannot crack this.. How do I ''loop'' through both lists and compare each item side by side in order then return the one's in list two that do not match the control list(list one)?

Logically the program would return ''More work'' as the wrong entry, because both "music" and "more work" are in the third row of their respective lists, they get checked and do not match. List 1 is control, so list 2's entry gets recorded as the odd one out.

Now I have tried it right and left, but I cannot make it happen.. Anyone out there who would be willing to shed some light on this or perhap开发者_Go百科s even guide me through to the right answer?

Many thanks

EDIT: Added my code for both arrayLists, im only missing the comparison function...

No particular reason for using ArrayList, any suggestions that would make this process easier are very welcome.


For starters, let's use a more modern way to read the lines:

IEnumerable<string> list1 = File.ReadLines("file1");
IEnumerable<string> list2 = Directory.EnumerateFiles("folder", 
       SearchOption.AllDirectories);

And then the list of files from list2 that are not in list1 :

IList<string> difference = list2.Except(list1).ToList();


  ArrayList diffs = new ArrayList();
  for(int i=0;i<N;i++) {
  if(!one.Item(i).equals(two.Item(i))  )
     diffs.Add(two.Item(i));
  }

N = number of items coomon to both lists. diffs = the arraylist with the odd ones of list two

If you want only the first odd entry from list two , then add a break into the loop. Here , the type of objects in the list is assumed to have defined an appropriate equals(Object obj)method.


Between these you can get the results you want.

IEnumerable<object> result = one.ToArray().Intersect(two.ToArray()); //Gives you what is the same
one.ToArray().Except(two.ToArray()); //Gives you wants in one not two
two.ToArray().Except(one.ToArray()); //Gives you wants in two and not in one


Here's a simple loop with integers that should work with any version the the framework.

If you have custom objects, here are guidelines for overriding Equals:

http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

 ArrayList left = new ArrayList();
 ArrayList right = new ArrayList();

 left.Add(1);
 left.Add(2);
 left.Add(3);

 right.Add(1);
 right.Add(2);
 right.Add(4);

 bool areEqual = CompareArrayList(left, right);

        private bool CompareArrayList(ArrayList left, ArrayList right)
        {

            if (left == null && right == null)
            {
                return true;
            }
            if (left == null || right == null)
            {
                return false;
            }
            if (left.Count != right.Count)
            {
                return false;
            }

            for (int i = 0; i < left.Count; i++)
            {
                if (!left[i].Equals(right[i]))
                {
                    return false;
                }
            }

            return true;

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜