2012-05-26 3 views
0

내가 hav 한 2D와 1D 배열을 비교했습니다 코드의 조각 .. 제발 어떻게 내가 결과를 저장할 수 있습니다 (두 요소 배열의 존재) 다른 2D 배열에 말해 줄래? 여기비교 후 2D 배열에 항목을 동적으로 추가하는 방법은 무엇입니까? C# 윈도우 양식

나는 "당신의 질문에서"이해 무엇
namespace WindowsFormsApplication1strng_cmp 
{ 
    public partial class Form1 : Form 
    { 
     private static Dictionary<string, Position> BuildDict(string[,] symbols) 
     { 
      Dictionary<string, Position> res = new Dictionary<string, Position>(); 
      for (int i = 0; i < symbols.GetLength(0); i++) 
      { 
       for (int j = 0; j < symbols.GetLength(1); j++) 
       { 
        res.Add(symbols[i, j], new Position(i, j)); 
       } 
      } 
      return res; 
     } 

     struct Position 
     { 
      public int x; 
      public int y; 
      public Position(int x, int y) 
      { 
       this.x = x; 
       this.y = y; 
      } 
     } 

     private static List<string> CompareUsingBrute(string[] text, string[,] symbols) 
     { 
      List<string> res = new List<string>(); 
      for (int x = 0; x < symbols.GetLength(0); x++) 
      { 
       for (int y = 0; y < symbols.GetLength(1); y++) 
       { 
        for (int z = 0; z < text.Length; z++) 
        { 
         if (symbols[x, y] == text[z]) 
          res.Add(text[z]); 

        } 

       } 
      } 
      return res; 

     } 
     string[,] symbols = new string[,] { { "if", "else" }, { "for", "foreach" }, { "while", "do" } }; 
     string[] text = new string[] { "for", "int", "in", "if", "then" }; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Dictionary<string, Position> dictionary = BuildDict(symbols); 
      // IndexElement[] index = BuildIndex(symbols); 

      foreach (string s in CompareUsingBrute(text, symbols)) 
      { 
       listBox2.Items.Add("valid"); 
      } 


     } 
    } 
} 
+0

하십시오로 아래의 출력을 연결 했습니까? –

답변

0

두 배열을 비교하기 위해 노력하고 일반적인 저장되어있는 코드를 내가 실제로 새로운 차원 배열에 "고해상도"를 SAV 할 입니다 ... 요소 -

는 예를 들어 입력/출력을 할 수 있습니다, 화상 -

class Program 
{ 
    static void Main(string[] args) 
    { 
     String[] array1 = {"a","b","c","d","e","f"}; 
     String[,] array2 = new String[,] {{"a","b","c","d"},{"d","e","f","g"}}; 

     List<String> array1AsList = array1.OfType<String>().ToList(); 
     List<String> array2AsList = array2.OfType<String>().ToList(); 

     // referring from **Ed S comments below**. Instead of using OFType(...) 
     // alternatively you can use the List<>().. ctor to convert an array to list 
     // although not sure for 2D arrays 

     var common = array1AsList.Intersect(array2AsList); 

    } 
} 

enter image description here

+0

왜 OfType ?을 (를) 호출합니까? 그것들은 둘 다'string' 배열이기 때문에 그들은 단지 문자열을 가질 수 있습니다 ... 또한, Enumerable을 취하는'List '생성자가 있습니다. –

+0

목록으로 변환하십시오. 다른 방법도있을 수 있습니다 .. –

+1

'var list = new List (array1); ' –

관련 문제