Re: [問題] 實現兩個陣列的聯集程式

看板C_Sharp作者 (LetMeGoogleThatForYou)時間16年前 (2010/03/18 11:22), 編輯推噓3(304)
留言7則, 3人參與, 最新討論串2/2 (看更多)
建議: 先讀完這個 提問的智慧 http://mis.ndhu.edu.tw/docu/question.htm ※ 引述《mopa (不要留下遺憾)》之銘言: : 有兩個字串陣列A[],B[] : 想寫一個取其聯集的程式 : C[] = A[] 聯集 B[] : 要如何下手呢? 101 LINQ Samples: Set Operators http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx Union - 1 This sample uses Union to create one sequence that contains the unique values from both arrays. public void Linq48() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var uniqueNumbers = numbersA.Union(numbersB); Console.WriteLine("Unique numbers from both arrays:"); foreach (var n in uniqueNumbers) { Console.WriteLine(n); } } Result Unique numbers from both arrays: 0 2 4 5 6 8 9 1 3 7 -- "Mr. Data, can you show me how to parse this XML?" "Certainly, sir. Let me google that for you." -- StarTrek, the Next Gen. 《Back to 2009》 (誤) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 65.87.177.87

03/18 13:32, , 1F
學了LINQ之後世界都不一樣了...XD
03/18 13:32, 1F

03/18 16:35, , 2F
LINQ 學習中+1
03/18 16:35, 2F

03/19 14:51, , 3F
直接使用的話......
03/19 14:51, 3F

03/19 14:52, , 4F
string[] A = new string[A的個數];
03/19 14:52, 4F

03/19 14:53, , 5F
string[] B = new string[B的個數];
03/19 14:53, 5F

03/19 14:53, , 6F
string[] C = A.Union<string>(B).ToArray<string>();
03/19 14:53, 6F

03/19 14:53, , 7F
C 就是結果 @@
03/19 14:53, 7F
文章代碼(AID): #1BePoF88 (C_Sharp)
文章代碼(AID): #1BePoF88 (C_Sharp)