C#使用自定義算法對(duì)數(shù)組進(jìn)行反轉(zhuǎn)操作的方法
更新時(shí)間:2015年04月06日 16:54:47 作者:令狐不聰
這篇文章主要介紹了C#使用自定義算法對(duì)數(shù)組進(jìn)行反轉(zhuǎn)操作的方法,涉及C#針對(duì)數(shù)組操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#使用自定義算法對(duì)數(shù)組進(jìn)行反轉(zhuǎn)操作的方法。分享給大家供大家參考。具體如下:
C#的Array對(duì)象自帶反轉(zhuǎn)功能,但是下面的代碼完全通過(guò)自定義的算法來(lái)實(shí)現(xiàn)數(shù)組反轉(zhuǎn)
復(fù)制代碼 代碼如下:
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");
}
}
{
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");
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# Newtonsoft.Json庫(kù)的常用屬性和方法詳解
Newtonsoft.Json(也稱為Json.NET)是一個(gè)非常流行的用于處理JSON數(shù)據(jù)的庫(kù),它提供了豐富的屬性和方法,用于序列化和反序列化JSON數(shù)據(jù),下面將通過(guò)C#代碼詳細(xì)講解一些常用的屬性和方法,并提供詳細(xì)的代碼注釋,需要的朋友可以參考下2025-03-03
C#實(shí)現(xiàn)設(shè)置或屏蔽熱鍵的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置或屏蔽熱鍵,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#.NET實(shí)現(xiàn)網(wǎng)頁(yè)自動(dòng)登錄的方法
這篇文章主要介紹了C#.NET實(shí)現(xiàn)網(wǎng)頁(yè)自動(dòng)登錄的方法,以實(shí)例形式分析了C#實(shí)現(xiàn)點(diǎn)擊自動(dòng)登錄的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
C#的WebBrowser的操作與注意事項(xiàng)介紹
C#的WebBrowser的操作與注意事項(xiàng)介紹,需要的朋友可以參考一下2013-03-03

