看板 C_Sharp 關於我們 聯絡資訊
抱歉,因為是自修,為了想要精進自己,想要多看大家的寫法。 閱讀書籍為 松崗的 visual c# 2013學習經典 目前在第三章 試將一個整數數列先作排序,再將重複的數值刪除。 如下圖所示 1.陣列的初值:23,12,34,12,45,12,23, 2.刪除重複數值後:12,23,34,45, 自己是寫出來了,但覺得有點冗,應該有很多種很簡潔的寫法。 class Program { private static void fliter (ref int[]ary) { int[] newary={ary[0]}; int inspect = ary[0]; for (int i = 0; i <= ary.Length - 1; i++) { if (inspect < ary[i]) { Array.Resize(ref newary, newary.Length + 1); newary[newary.Length - 1] = ary[i]; inspect = ary[i]; } } Console.Write("2.刪除重複數值後:"); foreach (int element in newary) Console.Write("{0}, ",element); } static void Main(string[] args) { int[] ary = new int[] { 23, 12, 34, 12, 45, 12, 23 }; Console.Write("1.陣列的初值:"); foreach (int element in ary) Console.Write("{0}, ",element); Console.WriteLine(); Array.Sort(ary); fliter(ref ary); Console.Read(); } } 麻煩大家分享及教導 感恩 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.25.152.197
YahooTaiwan:學到LINQ了嗎? 03/03 19:54
沒有,但剛Google看了一下,好像有點初始概念 但還是煩請指教。 ※ 編輯: e002311 來自: 114.25.152.197 (03/03 19:58)
m339606:用List http://ppt.cc/fyta 03/03 20:29
m339606:印象中有一個類似List的類別會自動在Add時判斷重複不插入 03/03 20:32
m339606:有可能記錯了...可能是Java的 03/03 20:33
m339606:算是比較開掛的方式XD 書中應該是要你練習迴圈吧 03/03 20:35
YahooTaiwan:ary.OrderBy(o => o).ToArray(); 03/03 20:39
YahooTaiwan:orderedArray.Distinct().ToArray(); 03/03 20:39
totte:Dictionary 03/03 22:22
感恩大大們 我現在就來消化看看 ※ 編輯: e002311 來自: 114.25.152.197 (03/05 11:19)