網站首頁 編程語言 正文
在學習.Net/C#或者任何一門面向對象語言的初期,大家都寫過交換兩個變量值,通常是通過臨時變量來實現。本篇使用多種方式實現兩個變量值的交換。
假設int x =1; int y = 2;現在交換兩個變量的值。
使用臨時變量實現
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("x={0},y={1}",x, y);
int temp = x;
x = y;
y = temp;
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
使用加減法實現
試想, 1+2=3,我們得到了兩數相加的結果3。3-2=1,把1賦值給y,y就等于1; 3-1=2,把2賦值給x,這就完成了交換。
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("x={0},y={1}",x, y);
x = x + y; //x = 3
y = x - y; //y = 1
x = x - y; //x = 2
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
使用ref和泛型方法實現
如果把交換int類型變量值的算法封裝到方法中,需要用到ref關鍵字。
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("x={0},y={1}",x, y);
Swap(ref x, ref y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = x;
}
如果交換string類型的變量值,就要寫一個Swap方法的重載,讓其接收string類型:
static void Main(string[] args)
{
string x = "hello";
string y = "world";
Console.WriteLine("x={0},y={1}",x, y);
Swap(ref x, ref y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = x;
}
static void Swap(ref string x, ref string y)
{
string temp = x;
x = y;
y = x;
}
如果交換其它類型的變量值呢?我們很容易想到通過泛型方法來實現,再寫一個泛型重載。
static void Main(string[] args)
{
string x = "hello";
string y = "world";
Console.WriteLine("x={0},y={1}",x, y);
Swap<string>(ref x, ref y);
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = x;
}
static void Swap(ref string x, ref string y)
{
string temp = x;
x = y;
y = x;
}
static void Swap<T>(ref T x, ref T y)
{
T temp = x;
x = y;
y = temp;
}
使用按位異或運算符實現
對于二進制數字來說,當兩個數相異的時候就為1, 即0和1異或的結果是1, 0和0,以及1和1異或的結果是0。關于異或等位運算符的介紹在這里:https://www.jb51.net/article/260847.htm
舉例,把十進制的3和4轉換成16位二進制分別是:
x = 0000000000000011;//對應十進制數字3
y = 0000000000000100; //對應十進制數字4
把x和y異或的結果賦值給x:x = x ^ y;
x = 0000000000000111;
把y和現在的x異或,結果賦值給y:y = y ^ x
y = 0000000000000011;
把現在的x和現在的y異或,結果賦值給x:x = x ^ y
x = 0000000000000100;
按照上面的算法,可以寫成如下:
static void Main(string[] args)
{
int x = 1;
int y = 2;
Console.WriteLine("x={0},y={1}",x, y);
x = x ^ y;
y = y ^ x;
x = x ^ y;
Console.WriteLine("x={0},y={1}", x, y);
Console.ReadKey();
}
原文鏈接:https://www.cnblogs.com/darrenji/p/4464799.html
相關推薦
- 2022-08-26 Python?pandas替換指定數據的方法實例_python
- 2023-03-29 Python-apply(lambda?x:?)的使用及說明_python
- 2023-03-20 C#?獲取XML文件內容的多種方式總結_C#教程
- 2022-03-03 實現不需要手動浮空瀏覽器緩存,程序可以獲取最新版本
- 2022-07-24 Python實現FIFO緩存置換算法_python
- 2024-03-15 Gitea Webhook報錯 webhook.ALLOWED_HOST_LIST setting
- 2022-06-21 c語言詳解動態內存分配及常見錯誤的解決_C 語言
- 2022-05-29 C#中使用HttpPost調用WebService的方法_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支