2011-09-28 7 views
1

두 개의 목록을 비교하고 차이가 있으면이를 반환하는 간단한 앱을 작성하고 있습니다.두 개의 ArrayList에있는 항목의 비교 및 ​​반환 차이

주 -이 설명

//---------------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------------------------ 

나는 모든 노력을하지만 나는이 균열 수 없다는 것을 목적으로 [INT] 가상 인덱스 .. 나는 '두 목록을 통해' '루프'를 어떻게하고 각 항목을 나란히 비교 한 다음 컨트롤 목록 (목록 하나)과 일치하지 않는 목록 2에있는 항목을 반환 하시겠습니까?

논리적으로 "음악"과 "더 많은 작업"이 각각의 목록의 세 번째 행에 있기 때문에 "더 많은 작업"이 잘못된 항목으로 반환됩니다. 목록 1은 통제이므로 목록 2의 항목은 이상한 것으로 기록됩니다.

지금 나는 그것을 오른쪽과 왼쪽으로 시도했지만 일어날 수 없다. 누군가가 이것에 대해 밝히거나 기꺼이 올바른 답을 알려줄 사람이 있겠는가?

많은 감사

편집 : 모두 arrayLists 내 코드를 추가, 메신저만을 비교 함수 누락 ...

이 과정을 쉽게 만들 것, 어떤 제안의 ArrayList를 사용하는 특별한 이유는 매우 환영하지 않습니다 .

+2

왜 ArrayList입니까? 여전히 Fx 1.x를 사용하고 있습니까? LINQ를 사용할 수 있습니까? 그리고 실제 코드와 비슷한 것을 예제로 추가하십시오. 문서 도구는 배열입니까? _edit_ 버튼이 있습니다. –

+0

ArrayList를 사용하는 특별한 이유가 있습니까? – asawyer

+1

목록이 정렬되어 있습니까? '{1, 2, 4}'와'{1, 2, 3, 4}'리스트가 주어지면 결과는'3'이되어야합니까? 목록이 정렬되지 않으면 '{1, 2, 3}'은 (는)'{3, 2, 1} '과 같은 것으로 간주됩니까? –

답변

1

이의이 라인을 읽기 위해 더 현대적인 방법을 사용하자 list1에없는 파일 목록은 다음과 같습니다.

IList<string> difference = list2.Except(list1).ToList(); 
0
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 = 두 목록 모두에 대한 coomon 수. diffs = 목록 2의 홀수 기호가있는 arraylist

목록 2의 첫 번째 홀수 항목 만 원한다면 break을 루프에 추가하십시오. 여기서 목록의 객체 유형은 적절한 equals(Object obj) 메소드를 정의한 것으로 가정합니다.

0

이들 사이에서 원하는 결과를 얻을 수 있습니다.

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 
0

프레임 워크의 모든 버전에서 작동하는 정수가 포함 된 간단한 루프가 있습니다. 사용자 정의 개체가있는 경우

, 여기에 최우선을위한 가이드 라인 같음있다 :

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

:

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; 

     } 
관련 문제