【问题标题】:How can I swap two values of an array in C#?如何在 C# 中交换数组的两个值?
【发布时间】:2017-05-03 11:41:21
【问题描述】:

我有一个 int 数组,其中包含一些从索引 0 开始的值。我想交换两个值,例如,索引 0 的值应该与索引 1 的值交换。我如何在 C# 中做到这一点数组?

【问题讨论】:

    标签: c# .net arrays sorting swap


    【解决方案1】:

    使用一个元组

    int[] arr = { 1, 2, 3 };
    (arr[0], arr[1]) = (arr[1], arr[0]);
    Console.WriteLine(string.Format($"{arr[0]} {arr[1]} {arr[2]}")); // 2 1 3
    

    元组在 C# 7.0 中可用 - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples

    【讨论】:

    • 我已经更新了答案
    【解决方案2】:

    如果你真的只想交换,可以用这个方法:

    public static bool swap(int x, int y, ref int[] array){
        
            // check for out of range
            if(array.Length <= y || array.Length <= x) return false;
            
    
            // swap index x and y
            var temp = array[x];
            array[x] = array[y];
            array[y] = temp;    
    
    
            return true;
    }
    

    x 和 y 是要交换的数组索引。

    如果你想与任何类型的数组交换,那么你可以这样做:

    public static bool swap<T>(this T[] objectArray, int x, int y){
        
            // check for out of range
            if(objectArray.Length <= y || objectArray.Length <= x) return false;
            
            
            // swap index x and y
            T temp = objectArray[x];
            objectArray[x] = objectArray[y];
            objectArray[y] = temp ;
            
    
            return true;
    }
    

    你可以这样称呼它:

    string[] myArray = {"1", "2", "3", "4", "5", "6"};
            
    if(!swap<string>(myArray, 0, 1)) {
        Console.WriteLine("x or y are out of range!");
    }
    else {
        //print myArray content (values will be swapped)
    }
    

    【讨论】:

      【解决方案3】:

      您可以创建适用于任何数组的扩展方法

      public static void SwapValues<T>(this T[] source, long index1, long index2)
      {
          T temp = source[index1];
          source[index1] = source[index2];
          source[index2] = temp;
      }
      

      【讨论】:

        【解决方案4】:

        只交换两个值一次或想对整个数组做同样的事情:

        假设你只想交换两个,并且是整数类型,那么你可以试试这个:

            int temp = 0;
            temp = arr[0];
            arr[0] = arr[1];
            arr[1] = temp;
        

        【讨论】:

          【解决方案5】:
          static void SwapInts(int[] array, int position1, int position2)
          {      
              int temp = array[position1]; // Copy the first position's element
              array[position1] = array[position2]; // Assign to the second element
              array[position2] = temp; // Assign to the first element
          }
          

          调用这个函数并打印元素

          【讨论】:

            【解决方案6】:

            我刚刚写了类似的东西,所以这里是一个版本

            • 使用泛型,使其适用于整数、字符串等,
            • 使用扩展方法
            • 自带测试类

            享受:)

            [TestClass]
            public class MiscTests
            {
                [TestMethod]
                public void TestSwap()
                {
                    int[] sa = {3, 2};
                    sa.Swap(0, 1);
                    Assert.AreEqual(sa[0], 2);
                    Assert.AreEqual(sa[1], 3);
                }
            }
            
            public static class SwapExtension
            {
                public static void Swap<T>(this T[] a, int i1, int i2)
                {
                    T t = a[i1]; 
                    a[i1] = a[i2]; 
                    a[i2] = t; 
                }
            }
            

            【讨论】:

              【解决方案7】:

              XOR 运算符可以交换两个值(数学魔法),如下所示:

              public static void Swap(int[] a, int i, int k)
              {
                  a[i] ^= a[k];
                  a[k] ^= a[i];
                  a[i] ^= a[k];
              }
              

              【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-05-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-05-15
              • 2022-01-06
              相关资源
              最近更新 更多