用于交换数组中两个元素的函数不起作用
本文关键字:元素 两个 函数 不起作用 数组 交换 用于 | 更新日期: 2023-09-27 18:13:12
我是c#新手,我不明白为什么这段代码不起作用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char[] sw = "ab".ToCharArray();
swap(sw[0], sw[1]);
string end = new string(sw);
Console.Write(end);
}
static void swap(char a, char b)
{
char temp = a;
a = b;
b = temp;
}
}
}
我期望在主机上是"ba",但我得到"ab"。我能够找到不同的方法来解决这个问题,但我想知道的是这个代码中的错误是什么。谢谢你的帮助!
问题是swap
方法实际上只是操作a
和b
的本地副本。您需要通过引用传递参数。所以您可以这样定义swap
方法:
static void swap(ref char a, ref char b)
{
char temp = a;
a = b;
b = temp;
}
然后这样命名:
swap(ref sw[0], ref sw[1]);
应该这样修改(注意:在本例中,ref char[] arr
的前缀ref
主要是为了说明目的:默认情况下,数组将由ref
传递)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char[] sw = "ab".ToCharArray();
swap(0, 1, ref sw );
string end = new string(sw);
Console.Write(end);
}
static void swap(int indexA, int indexB, ref char[] arr)
{
char temp = arr[indexA];
arr[indexA] = arr[indexB];
arr[indexB] =temp;
}
}
}
一个更通用的数组交换函数:
public static void Swap<T>(this T[] array, int indexA, int indexB)
{
T temp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = temp;
}
还有一个交换多个数组元素的泛型函数:
public static void Swap<T>(this T[] array, int indexA, int indexB, int length)
{
while (length-- > 0)
Swap(array, indexA++, indexB++);
}
您的交换使用两种值类型并在变量之间交换值。这里没有任何东西会修改原始数组。您需要将swap方法修改为如下内容:
static void Swap(char[] array, int a, int b)
{
char temp = array[a];
array[a] = array[b];
array[b] = temp;
}
你可以从Main()中调用它,像
Swap(array, 0, 1);
按值传递参数a
和b
。
参见按引用传递与按值传递的区别是什么?查看更多信息。
这里有两个解决方案来解决你的问题。
//Pass by value and return the values
static Tuple<char, char> swap2(char a, char b)
{
char temp = a;
a = b;
b = temp;
return new Tuple<char, char>(a, b);
}
//Pass by reference
static void swap3(ref char a, ref char b)
{
char temp = a;
a = b;
b = temp;
}
public static void Main(string[] args)
{
char[] sw2 = "ab".ToCharArray();
var chars2 = swap2(sw2[0], sw2[1]);
sw2[0] = chars2.Item1;
sw2[1] = chars2.Item2;
//Will print "ba"
Console.WriteLine(sw2);
char[] sw3 = "ab".ToCharArray();
swap3(ref sw3[0], ref sw3[1]);
//Will print "ba"
Console.WriteLine(sw3);
}
这里有一个关于是否应该使用或尽量避免ref关键字的问题。除了最简单的用法外,通常建议尽可能避免使用ref。Swap属于"最简单用途"的范畴,但我建议您尽量避免在大多数实际情况下使用ref。
什么时候使用c# ref关键字是一个好主意?