[問題] 新手習題問題

作者: e002311 (鴻)   2014-03-03 19:40:39
抱歉,因為是自修,為了想要精進自己,想要多看大家的寫法。
閱讀書籍為 松崗的 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();
}
}
麻煩大家分享及教導 感恩
作者: YahooTaiwan (超可愛南西我老婆)   2014-03-03 19:54:00
學到LINQ了嗎?
作者: m339606 (mize)   2014-03-03 20:29:00
用List http://ppt.cc/fyta印象中有一個類似List的類別會自動在Add時判斷重複不插入有可能記錯了...可能是Java的算是比較開掛的方式XD 書中應該是要你練習迴圈吧
作者: YahooTaiwan (超可愛南西我老婆)   2014-03-03 20:39:00
ary.OrderBy(o => o).ToArray();orderedArray.Distinct().ToArray();
作者: totte (totte)   2014-03-03 22:22:00
Dictionary

Links booklink

Contact Us: admin [ a t ] ucptt.com