C#使用自定義算法對數(shù)組進行反轉(zhuǎn)操作的方法
來源:易賢網(wǎng) 閱讀:1136 次 日期:2015-04-24 11:13:29
溫馨提示:易賢網(wǎng)小編為您整理了“C#使用自定義算法對數(shù)組進行反轉(zhuǎn)操作的方法”,方便廣大網(wǎng)友查閱!

C#的Array對象自帶反轉(zhuǎn)功能,但是下面的代碼完全通過自定義的算法來實現(xiàn)數(shù)組反轉(zhuǎn)

代碼如下:

public static void ReverseArray<T>(this T[] inputArray)

{

T temp = default(T);

if (inputArray == null)

throw new ArgumentNullException("inputArray is empty");

if (inputArray.Length > 0)

{

for (int counter = 0; counter < (inputArray.Length / 2); counter++)

{

temp = inputArray[counter];

inputArray[counter] = inputArray[inputArray.Length - counter - 1];

inputArray[inputArray.Length - counter - 1] = temp;

}

}

else

{

Trace.WriteLine("Reversal not needed");

}

}

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權(quán)所有:易賢網(wǎng)